如何获取每个唯一ID的摘要

时间:2017-04-27 00:33:09

标签: r unique plyr

我想在多列中提取一些值的摘要统计信息。我的数据如下所示

id                pace       type                   value      abundance 
51                (T)        (JC)                   (L)           0        
51                (T)        (JC)                   (L)           0 
51                (T)        (JC)                   (H)           0
52                (T)        (JC)                   (H)           0
52                (R)        (JC)                   (H)           0
53                (T)        (JC)                   (L)           1
53                (T)        (JC)                   (H)           1
53                (R)        (JC)                   (H)           1
53                (R)        (JC)                   (H)           1
53                (R)        (JC)                   (H)           1
54                (T)        (BC)                 <blank>         0          

54                (T)        (BC)                 <blank>         0 
54                (T)        (BC)                 <blank>         0

我希望有类似的东西

id    ptype       (T)    (R)        (L)      (H)     abundance
51     (JC)        3      0          2        1         0
52     (JC)        1      1          0        2         0
53     (JC)        2      3          1        4         1
54     (BC)        3      0          0        0         0

我已经开始编写一些代码了:

for (i in levels(df$id))
{
  extract.event <- df[df$id==i,]# To identify each section
ppace <- table(extract.event$pace) #count table of pace 
ptype <- extract.event$type[1] # extract the first line to be the type
nvalues <- table(extract.event$value) #count table of value
nabundance <- min(extract.event$abundance) #minimum of abundance

d <- cbind(ppace,ptype,forbeh,nvalues,nabundance)

但是我遇到了合并值的问题,特别是当nabundance打印出一个空表时。我不希望通过名称提取,因为数据框中有这么多名称。有任何想法吗?我认为它可能与plyr包有关,但仍不确定......

谢谢,

格雷斯

1 个答案:

答案 0 :(得分:3)

我不得不重写你的data.frame(以备参考,请粘贴dput的结果,因为我们讨厌重写你的数据),但这是我的尝试。我猜你正在寻找集合函数的一些东西:

df <- data.frame(id = as.factor(c(51,51,51,52,52,53,53,53,53,53,54,54,54)), 
      pace = c("(T)","(T)","(T)","(T)","(R)","(T)","(T)","(R)","(R)","(R)","(T)","(T)","(T)"), 
      type = c("(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(JC)","(BC)","(BC)","(BC)"), value = c("(L)","(L)","(H)","(H)","(H)","(L)","(H)","(H)","(H)","(H)","<blank>","<blank>","<blank>"), 
      abundance = c(0,0,0,0,0,1,1,1,1,1,0,0,0))

smallnames <- colnames(do.call("cbind",as.list(aggregate(cbind(value, pace, abundance) ~ id + type, data = lapply(df, as.character), table))))
smallnames
[1] "id"      "type"    "(H)"     "(L)"     "<blank>" "(R)"     "(T)"     "0"      
[9] "1"

df.new <- do.call("data.frame", as.list(aggregate(cbind(value, pace, abundance) ~ id + type, data = lapply(df, as.character), table)))
colnames(df.new) <- smallnames
df.new$abundance <- df.new$`1`
df.new
  id type (H) (L) <blank> (R) (T) 0 1 abundance
1 54 (BC)   0   0       3   0   3 3 0         0
2 51 (JC)   1   2       0   0   3 3 0         0
3 52 (JC)   2   0       0   1   1 2 0         0
4 53 (JC)   4   1       0   3   2 0 5         5

df.final <- df.new[, -which(colnames(df.new) %in% c("<blank>","0","1"))]
df.final
  id type (H) (L) (R) (T) abundance
1 54 (BC)   0   0   0   3         0
2 51 (JC)   1   2   0   3         0
3 52 (JC)   2   0   1   1         0
4 53 (JC)   4   1   3   2         5

如果您正在寻找这个或者您遇到问题,请告诉我。