【发布时间】:2017-10-11 13:32:16
【问题描述】:
这是我尝试使用 cast 函数对其进行透视的数据框 df
dput(df)
structure(list(Val = c(1L, 2L, 2L, 5L, 2L, 5L), `Perm 1` = structure(c(1L,
2L, 3L, 3L, 3L, 3L), .Label = c("Blue", "green", "yellow"
), class = "factor"), `Perm 2` = structure(c(1L, 2L, 2L, 3L,
3L, 3L), .Label = c("Blue", "green", "yellow"), class = "factor"),
`Perm 3` = structure(c(1L, 2L, 2L, 2L, 3L, 3L), .Label = c("Blue",
"green", "yellow"), class = "factor")), .Names = c("Val",
"Perm 1", "Perm 2", "Perm 3"), row.names = c(NA, 6L), class = "data.frame")
并期待数据透视后的数据
Blue 1 1 1
green 2 4 9
yellow 14 12 7
我试过了
cast(df, df$Val ~ df$`Perm 1`+df$`Perm 2`+df$`Perm 3`, sum, value = 'Val')
但这会出错
Error: Casting formula contains variables not found in molten data: df$Val, df$`Perm1`, df$`Perm2`
我怎样才能进行枢轴,以便我能够获得所需的 O/P
P.S- 数据框 DF 大约有 36 列,但为简单起见,我只取了 3 列。
任何建议将不胜感激。
谢谢
多尼克
【问题讨论】: