【发布时间】:2010-03-03 09:45:15
【问题描述】:
我在reshape 包中的cast/melt 中遇到了一个奇怪的行为。如果我投了data.frame,然后尝试melt,melt 就会出错。从演员data.frame 中手动取消设置“df.melt”类可以使其正确熔化。
有谁知道这是否是预期的行为,如果是,你想要它的用例是什么?
一个显示行为的小代码示例:
> df <- data.frame(type=c(1, 1, 2, 2, 3, 3), variable="n", value=c(71, 72, 68, 80, 21, 20))
> df
type variable value
1 1 n 71
2 1 n 72
3 2 n 68
4 2 n 80
5 3 n 21
6 3 n 20
> df.cast <- cast(df, type~., sum)
> names(df.cast)[2] <- "n"
> df.cast
type n
1 1 143
2 2 148
3 3 41
> class(df.cast)
[1] "cast_df" "data.frame"
> melt(df.cast, id="type", measure="n")
type value value
X.all. 1 143 (all)
X.all..1 2 148 (all)
X.all..2 3 41 (all)
> class(df.cast) <- "data.frame"
> class(df.cast)
[1] "data.frame"
> melt(df.cast, id="type", measure="n")
type variable value
1 1 n 143
2 2 n 148
3 3 n 41
【问题讨论】:
-
我很困惑。你为什么要
melt一个已经是长格式的df?而且您对cast的使用也没有多大意义。通常你使用它之后你使用melt。 -
请详细解释一下您尝试执行的操作以及您预期的结果。