【发布时间】:2018-06-22 12:25:54
【问题描述】:
有什么方法可以保留正在融化的变量的原始级别的名称?例如,在下面的示例中,有什么方法可以得到“alpha”、“beta”和“gamma”,而不是“1”、“2”、“3”。
当然,我可以重命名它们,但我正在使用的数据集有大量关卡,因此重命名它们既费时又容易出错。
谢谢。
library(data.table)
#> Warning: package 'data.table' was built under R version 3.4.2
set.seed(2334)
# define the dataframe
df <-
as.data.frame(
cbind(
a_alpha = rnorm(10),
a_beta = rnorm(10),
a_gamma = rnorm(10),
b_alpha = rnorm(10),
b_beta = rnorm(10),
b_gamma = rnorm(10),
id = c(1:10)
)
)
# check the structure of the wide format data
str(df)
#> 'data.frame': 10 obs. of 7 variables:
#> $ a_alpha: num -0.118 1.237 0.809 -0.766 -0.592 ...
#> $ a_beta : num 0.0019 1.0639 2.336 0.9056 0.6449 ...
#> $ a_gamma: num 0.5485 0.8345 -0.5977 0.0827 0.2754 ...
#> $ b_alpha: num 0.209 -0.305 0.434 -0.362 0.412 ...
#> $ b_beta : num -1.6404 2.8382 0.0661 0.7249 -0.4421 ...
#> $ b_gamma: num -0.144 0.964 -0.763 -1.356 0.995 ...
#> $ id : num 1 2 3 4 5 6 7 8 9 10
# convert to long format
df_long <- data.table::melt(
data.table::setDT(df),
measure = patterns("^a_", "^b_"),
value.name = c("a", "b"),
variable.name = "item"
)
# check the structure of the long format data
str(df_long)
#> Classes 'data.table' and 'data.frame': 30 obs. of 4 variables:
#> $ id : num 1 2 3 4 5 6 7 8 9 10 ...
#> $ item: Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
#> $ a : num -0.118 1.237 0.809 -0.766 -0.592 ...
#> $ b : num 0.209 -0.305 0.434 -0.362 0.412 ...
#> - attr(*, ".internal.selfref")=<externalptr>
# structure of item
levels(df_long$item)
#> [1] "1" "2" "3"
# Question: instead of "1" "2" "3", how to get the "item" factor levels to be: "alpha" "beta" "gamma"
由reprex package (v0.1.1.9000) 于 2018 年 1 月 12 日创建。
【问题讨论】:
-
我不确定您认为
data.table会从哪里获得"alpha" "beta" "gamma"字符串,因为它们在您的原始表中不存在(以那种形式)。 -
看看this 以获得一些灵感。您基本上必须使用
factor或match或类似的东西。 -
我认为这是两个包如何找到列的结果...
data.table的grep过于灵活,无法可靠地提取所需的列名部分(尽管它可以在你的情况下工作)。而sep版本可以隐式发送到strsplit以根据需要将字符串分开。由于简约,灵活性就丧失了。 -
@Henrik 非常感谢!!!这对我完全有用:)
-
@IndrajeetPatil 请注意我已更新my answer in the linked question。从
data.table 1.14.1开始,新函数measure保留了连接变量名的原始字符串。
标签: r data.table reshape melt