Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_Aggregate_Longitudinal - Fatal编程技术网

R 如何在两个分组变量(分组中分组)内聚合数据?

R 如何在两个分组变量(分组中分组)内聚合数据?,r,aggregate,longitudinal,R,Aggregate,Longitudinal,我想以长格式聚合数据。我有一个ID变量,一个年份变量,还有另外两个有趣的变量。我现在想在一年内对一个人的变量进行聚合 这是我的原始数据框的外观: ID year month x y 1 A 2014 3 2 NA 2 B 2010 2 3 NA 3 B 2010 5 NA 21 4 B 2011 2 2 NA 5 B 2011 5 NA 2

我想以长格式聚合数据。我有一个ID变量,一个年份变量,还有另外两个有趣的变量。我现在想在一年内对一个人的变量进行聚合

这是我的原始数据框的外观:

   ID year month      x    y
 1 A  2014     3      2    NA
 2 B  2010     2      3    NA
 3 B  2010     5     NA    21
 4 B  2011     2      2    NA
 5 B  2011     5     NA    25
 6 C  2012     5     NA    23
 7 C  2013     2      2    NA
 8 C  2013     5     NA    22
 9 C  2014     2      1    NA
10 C  2014    11     NA    30
这就是我想要的:

   ID year month      x1   y1
 1 A  2014     3      2    NA
 2 B  2010     2      3    21
 3 B  2010     5      3    21
 4 B  2011     2      2    25
 5 B  2011     5      2    25
 6 C  2012     5      NA   23
 7 C  2013     2      2    22
 8 C  2013     5      2    22
 9 C  2014     2      1    30
10 C  2014    11      1    30
每当一个人在一年内有两个度量值时(例如,B个人在2010年和2011年有两个度量值),我想将此人和每年的x和y值相加。稍后,我希望有一个数据框,每年只包含一行,但包含关于x和y的所有信息

像这样:

   ID year month      x1   y1
 1 A  2014     3      2    NA
 2 B  2010     5      3    21
 3 B  2011     2      2    25
 6 C  2012     5      NA   23
 7 C  2013     5      2    22
 9 C  2014     2      1    30

你对此有什么建议吗?非常感谢你的帮助

我们可以使用
na.locf0
from
zoo

library(dplyr)
library(zoo)
df1 %>%
   group_by(ID, year) %>%
   mutate_at(vars(x, y),  list(~ na.locf0(na.locf0(., fromLast = TRUE))))
# A tibble: 10 x 5
# Groups:   ID, year [6]
#   ID     year month     x     y
#   <chr> <int> <int> <int> <int>
# 1 A      2014     3     2    NA
# 2 B      2010     2     3    21
# 3 B      2010     5     3    21
# 4 B      2011     2     2    25
# 5 B      2011     5     2    25
# 6 C      2012     5    NA    23
# 7 C      2013     2     2    22
# 8 C      2013     5     2    22
# 9 C      2014     2     1    30
#10 C      2014    11     1    30
获取最终输出

df1 %>%
  group_by(ID, year) %>%
  fill(x, y, .direction = 'up') %>%       
  slice(1)
# A tibble: 6 x 5
# Groups:   ID, year [6]
#  ID     year month     x     y
#  <chr> <int> <int> <int> <int>
#1 A      2014     3     2    NA
#2 B      2010     2     3    21
#3 B      2011     2     2    25
#4 C      2012     5    NA    23
#5 C      2013     2     2    22
#6 C      2014     2     1    30
df1%>%
分组依据(ID,年份)%>%
填充(x,y,方向='向上')%>%
切片(1)
#一个tibble:6x5
#组:ID,年份[6]
#ID年份x月份y
#      
#1 A 2014 3 2不适用
#2 B 2010 2 3 21
#3 B 2011 2 25
#4 C 2012 5 NA 23
#5 C 2013 2 22
#6 C 2014 2 1 30
数据
df1我认为您需要将这两个变量列为:

聚合(x,by=list(ID,year),FUN=“yourfunctionhere”)

这同样适用于:

df %>%
  group_by(ID, year) %>%
  summarise_at(vars(one_of(c("x", "y"))), 
               ~ if (length(.x) == 1 && is.na(.x)) NA else sum(.x, na.rm = TRUE))

非常感谢!:)
df1 <- structure(list(ID = c("A", "B", "B", "B", "B", "C", "C", "C", 
 "C", "C"), year = c(2014L, 2010L, 2010L, 2011L, 2011L, 2012L, 
 2013L, 2013L, 2014L, 2014L), month = c(3L, 2L, 5L, 2L, 5L, 5L, 
 2L, 5L, 2L, 11L), x = c(2L, 3L, NA, 2L, NA, NA, 2L, NA, 1L, NA
 ), y = c(NA, NA, 21L, NA, 25L, 23L, NA, 22L, NA, 30L)),
  class = "data.frame", row.names = c("1", 
 "2", "3", "4", "5", "6", "7", "8", "9", "10"))
df %>%
  group_by(ID, year) %>%
  summarise_at(vars(one_of(c("x", "y"))), 
               ~ if (length(.x) == 1 && is.na(.x)) NA else sum(.x, na.rm = TRUE))