分组,汇总和转置

时间:2019-11-16 21:04:58

标签: r group-by summarize

我有一个看起来像这样的数据框:

ctgroup (dataframe)

Camera Trap Name  Animal Name         a_sum 
 1  CAM27             Chicken             1
 2  CAM27             Dog                 1
 3  CAM27             Dog                 4
 4  CAM28             Cat                 3
 5  CAM28             Dog                 22
 6  CAM28             Dog                 1

* a_sum =相机中记录的动物数量

所以本质上我想-按2个字段(“相机陷阱名称”,“科学名称”)分组,然后对“ a_sum”列中的记录数进行计数,然后对数据进行转置,以便“动物”。名称成为列,相机陷阱将我的行命名。我想在列中显示所有动物名称,如果没有可用数据,则显示0 即

Camera trap name        Dog   Cat   Wolf   Chicken
   CAM28                 23     4     1      4
   CAM27                 5      0     0      4

我尝试使用以下代码

dcast (ctgroup, Camera.Trap.name + Animal.name, value.var  = "a_sum")

我收到以下错误:

In dcast(ctgroup, Camera.Trap.name + Scientific.name, value.var = "a_sum") :
  The dcast generic in data.table has been passed a grouped_df and will attempt to redirect to the reshape2::dcast; please note that reshape2 is deprecated, and this redirection is now deprecated as well. Please do this redirection yourself like reshape2::dcast(ctgroup). In the next version, this warning will become an error.

我认为我不了解构建正确的代码来进行这项工作。

2 个答案:

答案 0 :(得分:2)

带有data.table ...

# Load data.table.
require(data.table)

# Create data.set.
df <- data.frame(Camera = c("CAM27", "CAM27", "CAM27", "CAM28", "CAM28", "CAM28"),
Animal = c("Chicken", "Dog", "Dog", "Cat", "Dog", "Dog"),
a_sum = c(1, 1, 4, 3, 22, 1))

# Set the data.frame as a data.table.
setDT(df)

# Cast by `Camera` and `Animal` and sum `a_sum`.
dcast(df, Camera ~ Animal, value.var = "a_sum", fun.aggregate = sum)
#    Camera Cat Chicken Dog
# 1:  CAM27   0       1   5
# 2:  CAM28   3       0  23

# If you want to coerce back to a data.frame.
setDF(df)


答案 1 :(得分:1)

dplyr方法:

library(dplyr)
library(tidyr)

ctgroup %>%
  group_by(Camera, Animal) %>%
  summarize(a_sum = sum(a_sum)) %>%
  pivot_wider(id_cols = Camera, names_from = Animal, values_from = a_sum, values_fill = list(a_sum = 0))