R 仅使用拆分变量的特定值的ddply

R 仅使用拆分变量的特定值的ddply,r,plyr,R,Plyr,是否可以仅为拆分变量的某些值返回ddply结果?例如,使用dataframe示例: example <- structure(list(shape = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("circle", "square", "triangle" ), class = "factor"), property = structure(c(1L, 3L, 2L, 1L

是否可以仅为拆分变量的某些值返回ddply结果?例如,使用dataframe
示例

example <- structure(list(shape = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("circle", "square", "triangle"
), class = "factor"), property = structure(c(1L, 3L, 2L, 1L, 
2L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 1L), .Label = c("color", 
"intensity", "size"), class = "factor"), value = structure(c(5L, 
2L, 1L, 5L, 4L, 1L, 5L, 6L, 6L, 7L, 4L, 3L, 6L, 5L), .Label = c("3", 
"5", "6", "7", "blue", "green", "red"), class = "factor")), .Names = c("shape", 
"property", "value"), class = "data.frame", row.names = c(NA, 
-14L))
我想返回一个数据框,其中包含具有特定颜色的每个形状的编号,如下所示:

    shape    property  blue green   red
1   circle   color     2    0       0
2   square   color     1    2       0
3   triangle color     1    1       1
ColorSummary <- ddply(example,.(shape,property="color"), function(example) summary(example$value))
然而,我似乎无法让它正常返回!我用这样的方法得到了一部分:

    shape    property  blue green   red
1   circle   color     2    0       0
2   square   color     1    2       0
3   triangle color     1    1       1
ColorSummary <- ddply(example,.(shape,property="color"), function(example) summary(example$value))
我做错了什么?有没有办法像我显示的第一个结果那样返回数据帧

此外,虽然这是一个小而快速的例子,但我的“真实”数据要大得多,并且需要很长时间来计算。ddply的速度是否仅限于
property=“color”

编辑:感谢您迄今为止的回答!不幸的是,我过分简化了情况,我不确定
dcast
解决方案是否适合我。让我解释一下-我实际上正在使用数据帧
示例2

example2 <- structure(list(factory = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), shape = structure(c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L), .Label = c("circle", 
"square", "triangle"), class = "factor"), property = structure(c(1L, 
3L, 2L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 3L, 2L
), .Label = c("color", "intensity", "size"), class = "factor"), 
    value = structure(c(5L, 2L, 1L, 5L, 4L, 1L, 5L, 6L, 6L, 7L, 
    4L, 3L, 6L, 5L, 5L, 2L, 1L), .Label = c("3", "5", "6", "7", 
    "blue", "green", "red"), class = "factor")), .Names = c("factory", 
"shape", "property", "value"), class = "data.frame", row.names = c(NA, 
-17L))
dcast(...~value,data=subset(example,property=='color'))
Aggregation function missing: defaulting to length
     shape property blue green red
1   circle    color    2     0   0
2   square    color    1     2   0
3 triangle    color    1     1   1

    factory shape   property    3   5   6   7   blue    green   red
1   A   circle  color   1   1   0   0   2   0   0
2   A   square  NA  1   0   0   1   1   2   0
3   A   triangle    NA  0   0   1   1   1   1   1
4   B   circle  NA  1   1   0   0   1   0   0
但我想返回的是以下内容(很抱歉,表格太乱,我在这里无法格式化表格):

这可能吗

编辑2:对于所有的编辑,我感到抱歉,我过于简化了我的情况。下面是一个更复杂的数据帧,它更接近我的真实示例。这个有一个列
state
,我不想使用它进行拆分。我可以用ddply(混乱地)实现这一点,但是我可以用dcast忽略
state

example3 <- structure(list(state = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("CA", "FL"
), class = "factor"), factory = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), shape = structure(c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L), .Label = c("circle", 
"square", "triangle"), class = "factor"), property = structure(c(1L, 
3L, 2L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 3L, 2L
), .Label = c("color", "intensity", "size"), class = "factor"), 
    value = structure(c(5L, 2L, 1L, 5L, 4L, 1L, 5L, 6L, 6L, 7L, 
    4L, 3L, 6L, 5L, 5L, 2L, 1L), .Label = c("3", "5", "6", "7", 
    "blue", "green", "red"), class = "factor")), .Names = c("state", 
"factory", "shape", "property", "value"), class = "data.frame", row.names = c(NA, 
-17L))

example3使用
dcast
来自
restrape2

example2 <- structure(list(factory = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), shape = structure(c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L), .Label = c("circle", 
"square", "triangle"), class = "factor"), property = structure(c(1L, 
3L, 2L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 3L, 2L
), .Label = c("color", "intensity", "size"), class = "factor"), 
    value = structure(c(5L, 2L, 1L, 5L, 4L, 1L, 5L, 6L, 6L, 7L, 
    4L, 3L, 6L, 5L, 5L, 2L, 1L), .Label = c("3", "5", "6", "7", 
    "blue", "green", "red"), class = "factor")), .Names = c("factory", 
"shape", "property", "value"), class = "data.frame", row.names = c(NA, 
-17L))
dcast(...~value,data=subset(example,property=='color'))
Aggregation function missing: defaulting to length
     shape property blue green red
1   circle    color    2     0   0
2   square    color    1     2   0
3 triangle    color    1     1   1
编辑 使用第二个数据集示例:

dcast(...~value,data=subset(example2,property=='color'))
Aggregation function missing: defaulting to length
  factory    shape property blue green red
1       A   circle    color    2     0   0
2       A   square    color    1     2   0
3       A triangle    color    1     1   1
4       B   circle    color    1     0   0

使用
重塑2
中的
dcast

example2 <- structure(list(factory = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("A", 
"B"), class = "factor"), shape = structure(c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L), .Label = c("circle", 
"square", "triangle"), class = "factor"), property = structure(c(1L, 
3L, 2L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 3L, 2L
), .Label = c("color", "intensity", "size"), class = "factor"), 
    value = structure(c(5L, 2L, 1L, 5L, 4L, 1L, 5L, 6L, 6L, 7L, 
    4L, 3L, 6L, 5L, 5L, 2L, 1L), .Label = c("3", "5", "6", "7", 
    "blue", "green", "red"), class = "factor")), .Names = c("factory", 
"shape", "property", "value"), class = "data.frame", row.names = c(NA, 
-17L))
dcast(...~value,data=subset(example,property=='color'))
Aggregation function missing: defaulting to length
     shape property blue green red
1   circle    color    2     0   0
2   square    color    1     2   0
3 triangle    color    1     1   1
编辑 使用第二个数据集示例:

dcast(...~value,data=subset(example2,property=='color'))
Aggregation function missing: defaulting to length
  factory    shape property blue green red
1       A   circle    color    2     0   0
2       A   square    color    1     2   0
3       A triangle    color    1     1   1
4       B   circle    color    1     0   0

restrape2
可能更适合此任务。包
restrape2
可能更适合此任务。感谢agstudy,这对我给出的示例非常有效。然而,我试图简化我的示例以使其变得简单。不幸的是,我还被另一个变量拆分,这就是为什么我要尝试ddply。我将编辑上面的问题,以显示我正在尝试的更复杂的情况。有没有办法使用dcast来完成这个新案例?非常感谢,agstudy!我真的很感谢你的帮助。您能告诉我是否有办法告诉dcast哪些列可以“拆分”吗?例如,我能否仅指明“factory”和“shape”列?问题是,我使用的是一个更大的数据帧,它有其他列,因此dcast解决方案没有正确计算工厂和形状。例如,假设我有另一列“state”,dcast将为我提供state/factory/shape的值,而我只需要factory/shape。我希望这是有意义的,如果不是,我可以添加另一个示例dataframe。再次感谢!亲爱的agstudy,我用一个新的数据框更新了我的原始问题,该数据框显示了一个我不想用于拆分(状态)的列。一个解决方法是创建只包含感兴趣的分组列的新数据框,但理想情况下,我希望只使用我的原始数据框。再次感谢agstudy的帮助。感谢agstudy,这对于我给出的示例非常有效。然而,我试图简化我的示例以使其变得简单。不幸的是,我还被另一个变量拆分,这就是为什么我要尝试ddply。我将编辑上面的问题,以显示我正在尝试的更复杂的情况。有没有办法使用dcast来完成这个新案例?非常感谢,agstudy!我真的很感谢你的帮助。您能告诉我是否有办法告诉dcast哪些列可以“拆分”吗?例如,我能否仅指明“factory”和“shape”列?问题是,我使用的是一个更大的数据帧,它有其他列,因此dcast解决方案没有正确计算工厂和形状。例如,假设我有另一列“state”,dcast将为我提供state/factory/shape的值,而我只需要factory/shape。我希望这是有意义的,如果不是,我可以添加另一个示例dataframe。再次感谢!亲爱的agstudy,我用一个新的数据框更新了我的原始问题,该数据框显示了一个我不想用于拆分(状态)的列。一个解决方法是创建只包含感兴趣的分组列的新数据框,但理想情况下,我希望只使用我的原始数据框。再次感谢你的帮助。