R 重塑:铸造古怪
要么已经晚了,要么我发现了一个bug,要么cast不喜欢colnames中带有“.”。这一切都发生在函数内部,但它在函数外部“不工作”,就像在函数内部不工作一样R 重塑:铸造古怪,r,R,要么已经晚了,要么我发现了一个bug,要么cast不喜欢colnames中带有“.”。这一切都发生在函数内部,但它在函数外部“不工作”,就像在函数内部不工作一样 x <- structure(list(df.q6 = structure(c(1L, 1L, 1L, 11L, 11L, 9L, 4L, 11L, 1L, 1L, 2L, 2L, 11L, 5L, 4L, 9L, 4L, 4L, 1L, 9L, 4L, 10L, 1L, 11L, 9L), .Label = c("a", "
x <- structure(list(df.q6 = structure(c(1L, 1L, 1L, 11L, 11L, 9L,
4L, 11L, 1L, 1L, 2L, 2L, 11L, 5L, 4L, 9L, 4L, 4L, 1L, 9L, 4L,
10L, 1L, 11L, 9L), .Label = c("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k"), class = "factor"), df.s5 = structure(c(4L,
4L, 1L, 2L, 4L, 4L, 4L, 3L, 4L, 1L, 2L, 1L, 2L, 4L, 1L, 3L, 4L,
2L, 2L, 4L, 4L, 4L, 2L, 2L, 1L), .Label = c("a", "b", "c", "d",
"e"), class = "factor")), .Names = c("df.q6", "df.s5"), row.names = c(NA,
25L), class = "data.frame")
cast(x, df.q6 + df.s5 ~., length)
x与colname中的点无关(很容易显示!)
如果您的数据帧没有名为“value”的列,那么cast()会猜测值是哪一列——在本例中,它会猜测“df.s5”,因为它是最后一列。这是融化()数据时得到的结果。然后,在调用Reforma1之前,它将该列重命名为“value”。现在“df.s5”列不存在了,但它在公式的左侧。哦
您正在使用公式中的值,这是一件奇怪的事情。演员阵容中没有一个能做到这一点。你想在这里干什么
您可以添加一个特殊列作为伪值:
> cast(cbind(x,1), df.q6+s5~., length)
使用1作为值列。使用值参数强制转换以覆盖此选择
df.q6 s5 (all)
1 a a 2
2 a b 2
3 a d 3
4 b a 1
5 b b 1
[etc]
但我怀疑有更好的方法来获取数据帧中重复观察(行)的数量——这才是真正的问题 如果您正在寻找一个简单的解决方案,重塑2软件包中的dcast可以帮助您:
library(reshape2)
dcast(x, df.q6 + df.s5 ~., length)
对我来说,我使用了与Spacedman指出的类似的解决方案
#take your data.frame x with it's two columns
#add a column
x$value <- 1
#apply your cast verbatim
cast(x, df.q6 + df.s5 ~., length)
df.q6 df.s5 (all)
1 a a 2
2 a b 2
3 a d 3
4 b a 1
5 b b 1
6 d a 1
7 d b 1
8 d d 3
9 e d 1
10 i a 1
11 i c 1
12 i d 2
13 j d 1
14 k b 3
15 k c 1
16 k d 1
#将您的data.frame x与两列合并
#添加一列
x$value在R2.11.1(W7)、重塑_0.8.3和plyr_1.2.1上的相同行为。以这种方式转换数据可以很容易地生成这样的图形:您在链接的线程中构建了问题的完整解决方案吗?stackoverflow.com/questions/2578961/没有机会浏览它,很好奇你是否需要一个标签机来制作图表?不需要标签机,只需要这个来填充=“谢谢你对重塑2的了解。本质上它需要“一些东西”来计算,即应用长度()。因为它是为melt()输出设置的,所以变量名“value”可以工作,但我认为还有什么其他的吗?剩余变量被认为是“值”变量?也在plyr中,可能是这样?ddply(x,(df.q6,df.s5),总结,计数=长度(df.q6))
#take your data.frame x with it's two columns
#add a column
x$value <- 1
#apply your cast verbatim
cast(x, df.q6 + df.s5 ~., length)
df.q6 df.s5 (all)
1 a a 2
2 a b 2
3 a d 3
4 b a 1
5 b b 1
6 d a 1
7 d b 1
8 d d 3
9 e d 1
10 i a 1
11 i c 1
12 i d 2
13 j d 1
14 k b 3
15 k c 1
16 k d 1