在 r 中变换 data.table
transform data.table in r
我有这样的数据:
data1=data.frame("long_customers"=c(TRUE, FALSE, FALSE, TRUE),
"long_partners"=c(FALSE, TRUE, FALSE, FALSE),
"short_customers"=c(FALSE, TRUE,TRUE,FALSE),
"short_partners"=c(FALSE,FALSE,NA,NA))
我想要 transform/reshape 这个看起来像:
data2=data.frame("x"=c("long","long","long","long","long","long","long","long", "short", "short", "short", "short", "short", "short", "short", "short"),
"y"=c("customers", "customers", "customers", "customer", "partners", "partners", "partners", "partners","customers", "customers", "customers", "customer", "partners", "partners", "partners", "partners"),
"event"=c(TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, NA, NA))
我怎么去那里?我将不胜感激任何指点,并希望获得一种 data.table 的方式来做到这一点。
谢谢!
您可以使用 pivot_longer
和 separate
:
library(tidyverse)
data1 %>%
pivot_longer(cols = everything()) %>%
separate(name, into = c("x", "y")) %>%
arrange(x, y)
# A tibble: 16 x 3
x y value
<chr> <chr> <lgl>
1 long customers TRUE
2 long customers FALSE
3 long customers FALSE
4 long customers TRUE
5 long partners FALSE
6 long partners TRUE
7 long partners FALSE
8 long partners FALSE
9 short customers FALSE
10 short customers TRUE
11 short customers TRUE
12 short customers FALSE
13 short partners FALSE
14 short partners FALSE
15 short partners NA
16 short partners NA
对于 data.table
解决方案,使用 melt
然后 tstrsplit
:
setDT(data1)
melt(data1, value.name="event",)[, c("x", "y") := tstrsplit(variable, "_")][,.(x,y,event)]
x y event
1: long customers TRUE
2: long customers FALSE
3: long customers FALSE
4: long customers TRUE
5: long partners FALSE
6: long partners TRUE
7: long partners FALSE
8: long partners FALSE
9: short customers FALSE
10: short customers TRUE
11: short customers TRUE
12: short customers FALSE
13: short partners FALSE
14: short partners FALSE
15: short partners NA
16: short partners NA
我有这样的数据:
data1=data.frame("long_customers"=c(TRUE, FALSE, FALSE, TRUE),
"long_partners"=c(FALSE, TRUE, FALSE, FALSE),
"short_customers"=c(FALSE, TRUE,TRUE,FALSE),
"short_partners"=c(FALSE,FALSE,NA,NA))
我想要 transform/reshape 这个看起来像:
data2=data.frame("x"=c("long","long","long","long","long","long","long","long", "short", "short", "short", "short", "short", "short", "short", "short"),
"y"=c("customers", "customers", "customers", "customer", "partners", "partners", "partners", "partners","customers", "customers", "customers", "customer", "partners", "partners", "partners", "partners"),
"event"=c(TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, NA, NA))
我怎么去那里?我将不胜感激任何指点,并希望获得一种 data.table 的方式来做到这一点。
谢谢!
您可以使用 pivot_longer
和 separate
:
library(tidyverse)
data1 %>%
pivot_longer(cols = everything()) %>%
separate(name, into = c("x", "y")) %>%
arrange(x, y)
# A tibble: 16 x 3
x y value
<chr> <chr> <lgl>
1 long customers TRUE
2 long customers FALSE
3 long customers FALSE
4 long customers TRUE
5 long partners FALSE
6 long partners TRUE
7 long partners FALSE
8 long partners FALSE
9 short customers FALSE
10 short customers TRUE
11 short customers TRUE
12 short customers FALSE
13 short partners FALSE
14 short partners FALSE
15 short partners NA
16 short partners NA
对于 data.table
解决方案,使用 melt
然后 tstrsplit
:
setDT(data1)
melt(data1, value.name="event",)[, c("x", "y") := tstrsplit(variable, "_")][,.(x,y,event)]
x y event
1: long customers TRUE
2: long customers FALSE
3: long customers FALSE
4: long customers TRUE
5: long partners FALSE
6: long partners TRUE
7: long partners FALSE
8: long partners FALSE
9: short customers FALSE
10: short customers TRUE
11: short customers TRUE
12: short customers FALSE
13: short partners FALSE
14: short partners FALSE
15: short partners NA
16: short partners NA