可以在没有聚合函数的情况下使用dcast吗? [重复]

时间:2021-08-19 08:15:47

Possible Duplicate:
This R reshaping should be simple, but

可能重复:这个R重塑应该很简单,但是

dcast from reshape2 works without a formula where there are no duplicates. Take these example data:

来自reshape2的dcast在没有重复项的公式的情况下工作。以这些示例数据为例:

df <- structure(list(id = c("A", "B", "C", "A", "B", "C"), cat = c("SS", 
"SS", "SS", "SV", "SV", "SV"), val = c(220L, 222L, 223L, 224L, 
225L, 2206L)), .Names = c("id", "cat", "val"), class = "data.frame", row.names = c(NA, 
-6L))

I'd like to dcast these data and just have the values tabulated, without applying any function to the value.var including the default length.

我想dcast这些数据,只是将值列表,而不对value.var应用任何函数,包括默认长度。

In this case, it works fine.

在这种情况下,它工作正常。

> dcast(df, id~cat, value.var="val")
  id  SS   SV
1  A 220  224
2  B 222  225
3  C 223 2206

But when there are duplicate variables, the fun defaults to length. Is there a way to avoid it?

但是当存在重复变量时,乐趣默认为长度。有没有办法避免它?

df2 <- structure(list(id = c("A", "B", "C", "A", "B", "C", "C"), cat = c("SS", 
"SS", "SS", "SV", "SV", "SV", "SV"), val = c(220L, 222L, 223L, 
224L, 225L, 220L, 1L)), .Names = c("id", "cat", "val"), class = "data.frame", row.names = c(NA, 
-7L))

> dcast(df2, id~cat, value.var="val")
Aggregation function missing: defaulting to length
  id SS SV
1  A  1  1
2  B  1  1
3  C  1  2

Ideally what I'm looking for is to add a fun = NA, as in don't try to aggregate the value.var. The result I'd like when dcasting df2:

理想情况下,我正在寻找的是添加一个fun = NA,就像不要尝试聚合value.var一样。在dcasting df2时我想要的结果:

 id  SS  SV
1  A 220 224
2  B 222 225
3  C 223 220
4. C NA  1

2 个解决方案

#1


19  

I don't think there is a way to do it directly but we can add in an additional column which will help us out

我认为没有办法直接进行,但我们可以添加一个额外的列来帮助我们

df2 <- structure(list(id = c("A", "B", "C", "A", "B", "C", "C"), cat = c("SS", 
"SS", "SS", "SV", "SV", "SV", "SV"), val = c(220L, 222L, 223L, 
224L, 225L, 220L, 1L)), .Names = c("id", "cat", "val"), class = "data.frame", row.names = c(NA, 
-7L))

library(reshape2)
library(plyr)
# Add a variable for how many times the id*cat combination has occured
tmp <- ddply(df2, .(id, cat), transform, newid = paste(id, seq_along(cat)))
# Aggregate using this newid and toss in the id so we don't lose it
out <- dcast(tmp, id + newid ~ cat, value.var = "val")
# Remove newid if we want
out <- out[,-which(colnames(out) == "newid")]
> out
#  id  SS  SV
#1  A 220 224
#2  B 222 225
#3  C 223 220
#4  C  NA   1

#2


9  

I figured out the same solution while Dason was answering mine.

当Dason回答我的时候,我想出了同样的解决方案。

I realized that dcast simply does not know how to deal with duplicates. The way I figured out how to trick it was by adding another unique identifer so it doesn't get confused by duplicates.

我意识到dcast根本不知道如何处理重复项。我弄清楚如何欺骗它的方式是添加另一个独特的标识符,这样它就不会被重复项混淆。

In this example:

在这个例子中:

df <- ddply(df2, .(cat), function(x){ x$id2 = 1:nrow(x); x})
>  dcast(df, id+id2~cat, value.var="val")[,-2]
  id  SS  SV
1  A 220 224
2  B 222 225
3  C 223 220
4  C  NA   1

#1


19  

I don't think there is a way to do it directly but we can add in an additional column which will help us out

我认为没有办法直接进行,但我们可以添加一个额外的列来帮助我们

df2 <- structure(list(id = c("A", "B", "C", "A", "B", "C", "C"), cat = c("SS", 
"SS", "SS", "SV", "SV", "SV", "SV"), val = c(220L, 222L, 223L, 
224L, 225L, 220L, 1L)), .Names = c("id", "cat", "val"), class = "data.frame", row.names = c(NA, 
-7L))

library(reshape2)
library(plyr)
# Add a variable for how many times the id*cat combination has occured
tmp <- ddply(df2, .(id, cat), transform, newid = paste(id, seq_along(cat)))
# Aggregate using this newid and toss in the id so we don't lose it
out <- dcast(tmp, id + newid ~ cat, value.var = "val")
# Remove newid if we want
out <- out[,-which(colnames(out) == "newid")]
> out
#  id  SS  SV
#1  A 220 224
#2  B 222 225
#3  C 223 220
#4  C  NA   1

#2


9  

I figured out the same solution while Dason was answering mine.

当Dason回答我的时候,我想出了同样的解决方案。

I realized that dcast simply does not know how to deal with duplicates. The way I figured out how to trick it was by adding another unique identifer so it doesn't get confused by duplicates.

我意识到dcast根本不知道如何处理重复项。我弄清楚如何欺骗它的方式是添加另一个独特的标识符,这样它就不会被重复项混淆。

In this example:

在这个例子中:

df <- ddply(df2, .(cat), function(x){ x$id2 = 1:nrow(x); x})
>  dcast(df, id+id2~cat, value.var="val")[,-2]
  id  SS  SV
1  A 220 224
2  B 222 225
3  C 223 220
4  C  NA   1