问题
I have the following dataframe, called DF,
Country Year Var1 Var2
USA 2010 5 3
USA 2011 6 5
USA 2012 NA 8
USA 2013 4 NA
USA 2014 NA 6
USA 2015 6 9
CHN 2010 NA 5
CHN 2011 7 NA
CHN 2012 6 NA
CHN 2013 4 4
CHN 2014 NA 6
CHN 2015 NA 8
EGY 2010 3 NA
EGY 2011 3 5
EGY 2012 3 6
EGY 2013 NA 8
EGY 2014 NA NA
EGY 2015 NA 2
I want to take a 3 year average of the data. However, if there are only two years of available data within a particular three year interval, I want to ignore the NA and take a two year average. Similarly, if data is only available for one year within a particular three year interval, I want to keep that data point as the "average" for that three year interval. Basically, within each three year interval, I want to take the mean, and ignoring the NAs.
I have tried the following solution recommended in : R: Calculating 5 year averages in panel data
int<-cut(DF$Year,seq(2010,2016,by=3),right=F)
id<-c("Var1", "Var2")
ag<-aggregate(DF[id],list(DF$Country,int), mean)
It yielded the following:
Group.1 Group.2 Var1 Var2
CHN [2010,2013) NA NA
EGY [2010,2013) 3 NA
USA [2010,2013) NA 5.333333
CHN [2013,2016) NA 6.000000
EGY [2013,2016) NA NA
USA [2013,2016) NA NA
But the output I am interested in is:
Group.1 Group.2 Var1 Var2
CHN [2010,2013) 6.5 5
EGY [2010,2013) 3 5.5
USA [2010,2013) 5.5 5.3
CHN [2013,2016) 4 6
EGY [2013,2016) NA 5
USA [2013,2016) 5 7.5
回答1:
Here's how you can do that with package dplyr
. Basically, you first create a "year group" using mutate
. I used ifelse
but it you have more groups, you should consider looking at case_when
although nested ifelse
will work. Then, we summarise by country and Year_group.
df1 <- read.table(text="Country Year Var1 Var2
USA 2010 5 3
USA 2011 6 5
USA 2012 NA 8
USA 2013 4 NA
USA 2014 NA 6
USA 2015 6 9
CHN 2010 NA 5
CHN 2011 7 NA
CHN 2012 6 NA
CHN 2013 4 4
CHN 2014 NA 6
CHN 2015 NA 8
EGY 2010 3 NA
EGY 2011 3 5
EGY 2012 3 6
EGY 2013 NA 8
EGY 2014 NA NA
EGY 2015 NA 2",header=TRUE, stringsAsFactors=FALSE)
library(dplyr)
df1%>%
group_by(Country)%>%
mutate(Year_group=ifelse(Year<2013,"2010-2012","2013-2016"))%>%
group_by(Country,Year_group)%>%
summarise(Mean_var1=mean(Var1,na.rm=TRUE),Mean_var2=mean(Var2,na.rm=TRUE)
Country Year_group Mean_var1 Mean_var2
<chr> <chr> <dbl> <dbl>
1 CHN 2010-2012 6.5 5.000000
2 CHN 2013-2016 4.0 6.000000
3 EGY 2010-2012 3.0 5.500000
4 EGY 2013-2016 NaN 5.000000
5 USA 2010-2012 5.5 5.333333
6 USA 2013-2016 5.0 7.500000
回答2:
You are almost there, only one addition to your code is required:
int <- cut(DF$Year, seq(2010, 2016, by = 3), right = FALSE)
id <- c("Var1", "Var2")
ag <- aggregate(DF[id], list(DF$Country, int), mean, na.rm = TRUE)
# |
#-----------------------------------------------------
ag
# Group.1 Group.2 Var1 Var2
#1 CHN [2010,2013) 6.5 5.000000
#2 EGY [2010,2013) 3.0 5.500000
#3 USA [2010,2013) 5.5 5.333333
#4 CHN [2013,2016) 4.0 6.000000
#5 EGY [2013,2016) NaN 5.000000
#6 USA [2013,2016) 5.0 7.500000
aggregate()
accepts further arguments passed to or used by methods. This way, you can pass the na.rm = TRUE
parameter to mean()
.
来源:https://stackoverflow.com/questions/44370180/taking-a-3-year-average-across-in-a-panel-data-set-with-nas