Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
管理一列中的重复条目,同时保持r中其他列的完整性_R_Dplyr_Aggregate_Plyr_Melt - Fatal编程技术网

管理一列中的重复条目,同时保持r中其他列的完整性

管理一列中的重复条目,同时保持r中其他列的完整性,r,dplyr,aggregate,plyr,melt,R,Dplyr,Aggregate,Plyr,Melt,我有多个地点的物种计数数据,包括地点、年、月和日的信息。在这些数据中,对于一些物种,在某些日子有多个条目。例如,2016年1月3日,物种A1有两个条目,即10和20。第一步,我想取今天的最大值,即20。在第二步中,如果每个站点每个月有超过一天的采样,那么我想取每个月的平均值。下面给出了一个例子 species site year month day total A1 GG 2016 1 3 10 A1 GG 2016 1 3 20 A1 GG

我有多个地点的物种计数数据,包括地点、年、月和日的信息。在这些数据中,对于一些物种,在某些日子有多个条目。例如,2016年1月3日,物种A1有两个条目,即10和20。第一步,我想取今天的最大值,即20。在第二步中,如果每个站点每个月有超过一天的采样,那么我想取每个月的平均值。下面给出了一个例子

species site    year    month   day total
A1  GG  2016    1   3   10
A1  GG  2016    1   3   20
A1  GG  2016    1   4   22
A2  GG  2016    1   5   32
A2  GG  2016    1   6   34
A3  GG  2016    1   9   23
应该是这样的

species site    year    month   day total
A1  GG  2016    1   3.5 21
A2  GG  2016    1   5.5 33
A3  GG  2016    1   9   23

我们按照前五列进行分组,即“物种”、“地点”、“年”、“月”、“日”、“总结”,以获得“总计”的
max
,然后按照没有“日”的情况进行分组,并获得“日”和“总计”的
平均值

library(dplyr)
df1 %>%
    group_by_at(names(.)[1:5]) %>% 
    summarise(total = max(total)) %>%
    group_by_at(names(.)[1:4]) %>%
    summarise_all(mean)
# A tibble: 3 x 6
# Groups: species, site, year [?]
#   species site   year month   day total
#   <chr>   <chr> <int> <int> <dbl> <dbl>
#1 A1      GG     2016     1  3.50  21.0
#2 A2      GG     2016     1  5.50  33.0
#3 A3      GG     2016     1  9.00  23.0
库(dplyr)
df1%>%
分组人(姓名(.)[1:5])%>%
汇总(总计=最大(总计))%>%
分组人(姓名(.)[1:4])%>%
总结所有(平均值)
#一个tibble:3x6
#组:物种、地点、年份[?]
#物种地点年月日总数
#          
#1 A1 GG 2016 1 3.50 21.0
#2 A2 GG 2016 1 5.50 33.0
#3 A3 GG 2016 1 9.00 23.0

以下是使用
数据的解决方案。表

> library(data.table)
> dt <- fread("
  species site    year    month   day total
  A1  GG  2016    1   3   10
  A1  GG  2016    1   3   20
  A1  GG  2016    1   4   22
  A2  GG  2016    1   5   32
  A2  GG  2016    1   6   34
  A3  GG  2016    1   9   23
  ")
> cols_with_day <- c('species', 'site', 'year', 'month', 'day')
> cols_without_day <- c('species', 'site', 'year', 'month')
> result <- dt[, .(total = max(total)), by = cols_with_day
               ][, .(day = mean(day), total = mean(total)), by = cols_without_day]
> result
   species site year month day total
1:      A1   GG 2016     1 3.5    21
2:      A2   GG 2016     1 5.5    33
3:      A3   GG 2016     1 9.0    23
>库(data.table)
>dt cols_有天cols_没有天结果
物种地点年月日总数
1:A1 GG 2016 1 3.5 21
2:A2 GG 2016 1 5.5 33
3:A3 GG 2016 1 9.0 23