我正在尝试找到使用字符列名融化数据框的解决方案,并继续在融化的 df 中获取因子变量。
我的数据的一个子集:
df <- structure(list(bloc = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L,
6L, 6L), name = c("Cristina", "Robijn", "Robijn", "Cristina",
"Robijn", "Cristina", "Cristina", "Robijn", "Robijn", "Cristina",
"Cristina", "Robijn"), d1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0), d7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), d10 = c(1, 0,
0, 5, 0, 0.1, 1, 0, 0, 0.1, 0.1, 0), d13 = c(0, 0, 0, 0, 0, 0.1,
0.1, 0, 0, 0.1, 0, 0.1), d20 = c(0.1, 1, 5, 5, 5, 50, 5, 5, 5,
5, 25, 1), d24 = c(75, 5, 25, 100, 25, 75, 95, 5, 5, 100, 75,
25), d27 = c(75L, 5L, 50L, 100L, 50L, 100L, 100L, 50L, 50L, 100L,
100L, 25L), d34 = c(75L, 25L, 75L, 100L, 75L, 100L, 100L, 75L,
75L, 100L, 100L, 50L), d41 = c(75L, 25L, 95L, 100L, 100L, 100L,
100L, 95L, 95L, 100L, 100L, 75L), d48 = c(100L, 50L, 95L, 100L,
75L, 100L, 100L, 75L, 95L, 100L, 100L, 95L), d55 = c(100L, 75L,
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L)), row.names = c(1L,
2L, 43L, 44L, 60L, 62L, 78L, 80L, 95L, 96L, 112L, 117L), class = "data.frame")
和代码:
data.table::melt(
df,
id.vars = c("bloc", "name"),
variable.name = "time",
value.name = "severity",
variable.factor = FALSE
) %>% str()
最佳答案
发生这种情况的原因是 df
是一个数据帧。在这种情况下,来自 data.table 的 melt
回退到来自 reshape2 的 melt
的行为,它没有 variable.factor
参数。
您可以在 data.table::melt
的源代码中看到这一点:
> data.table::melt
function (data, ..., na.rm = FALSE, value.name = "value")
{
if (is.data.table(data))
UseMethod("melt", data)
else reshape2::melt(data, ..., na.rm = na.rm, value.name = value.name)
}
<bytecode: 0x10f886b88>
<environment: namespace:data.table>
因此,您的代码:
melt(
df,
id.vars = c("bloc", "name"),
variable.name = "time",
value.name = "severity",
variable.factor = FALSE
) %>% str()
给你:
但是,当在
df
中包装 setDT
时:melt(
setDT(df),
id.vars = c("bloc", "name"),
variable.name = "time",
value.name = "severity",
variable.factor = FALSE
) %>% str()
您将获得所需的输出:
关于r - data.table::melt - 变量列转换为具有 variable.factor = FALSE 指定的因子,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51807913/