data.table :: melt-将变量列转换为具有variable.factor = FALSE指定的因数

时间:2018-08-12 10:22:46

标签: r data.table melt

我正试图找到解决方案来融合具有字符同名的数据框,并不断在融合的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()

1 个答案:

答案 0 :(得分:6)

之所以会这样,是因为df是一个数据帧。在这种情况下,来自melt会退回到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()

给您

'data.frame': 132 obs. of  4 variables:
 $ bloc    : int  1 1 2 2 3 3 4 4 5 5 ...
 $ name    : chr  "Cristina" "Robijn" "Robijn" "Cristina" ...
 $ time    : Factor w/ 11 levels "d1","d7","d10",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ severity: num  0 0 0 0 0 0 0 0 0 0 ...

但是,在df中包装setDT时:

melt(
  setDT(df),
  id.vars = c("bloc", "name"),
  variable.name = "time",
  value.name = "severity",
  variable.factor = FALSE
) %>% str()

您将获得所需的输出:

Classes ‘data.table’ and 'data.frame':    132 obs. of  4 variables:
 $ bloc    : int  1 1 2 2 3 3 4 4 5 5 ...
 $ name    : chr  "Cristina" "Robijn" "Robijn" "Cristina" ...
 $ time    : chr  "d1" "d1" "d1" "d1" ...
 $ severity: num  0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, ".internal.selfref")=<externalptr>