我正在尝试从每日价值计算月平均值。我的数据有太多缺失值,我想用NA值填充它们。 例如,这是所需的输出:
"MM","YY","RR"
10,1961,NA
10,1962,NA
10,1963,NA
10,1964,NA
10,1965,NA
10,1966,NA
10,1967,NA
10,1968,NA
10,1969,NA
10,1970,NA
10,1971,14.8290322580645
10,1972,5.92903225806452
10,1973,7.10645161290323
10,1974,9.25806451612903
10,1975,6.13225806451613
10,1976,NA
10,1977,NA
10,1978,NA
10,1979,11.358064516129
10,1980,NA
10,1981,20.8354838709677
10,1982,NA
10,1983,NA
10,1984,7.4741935483871
10,1985,NA
10,1986,NA
10,1987,NA
10,1988,NA
10,1989,NA
10,1990,NA
10,1991,NA
10,1992,NA
10,1993,NA
10,1994,NA
10,1995,NA
10,1996,NA
10,1997,NA
10,1998,NA
10,1999,NA
10,2000,NA
10,2001,12.2548387096774
10,2002,7.19354838709677
10,2003,4.34193548387097
10,2004,8.09354838709677
10,2005,10.3354838709677
10,2006,5.49677419354839
10,2007,9.58709677419355
10,2008,NA
10,2009,NA
10,2010,17.4548387096774
可以从此链接下载测试数据: Link to Data
我使用聚合函数计算平均值
以下是我的剧本:
library(plyr)
dat<- read.csv("test.csv",header=TRUE,sep=",")
dat[dat == -999]<- NA
dat[dat == -888]<- 0
monthly_mean<-aggregate(RR ~ MM + YY,dat,mean)
#Filter August Only
oct<-monthly_mean[which(monthly_mean$MM == 10),]
dat2 <- as.data.frame(oct)
#monthly_mean <- ddply(dat,.(MM, DD), sumaprise, mean_r =
mean(RR,na.rm=TRUE))
write.table(dat2,file="test_oct.csv",sep=",",col.names=T,row.names=F, na="NA")
问题:
[1]当我运行此脚本时,也会删除缺失的年份。
我很感激有关如何在R中正确执行此操作的任何建议。
答案 0 :(得分:3)
您可以通过将聚合函数更改为
来保留NA列monthly_mean<-aggregate(RR ~ MM + YY,dat,mean,na.action=na.pass)