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

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)

但是我在合并值时遇到了问题,尤其是在无用的状态下打印出一个空表时。我不希望按名称提取数据,因为数据框中有太多名称。有任何想法吗?我认为这可能与plyr软件包有关,但仍不确定...

谢谢,

优雅

最佳答案

我不得不重写您的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

让我知道这是您要查找的内容还是遇到问题。

关于r - 如何获取每个唯一ID的摘要,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43646725/

10-13 04:34