在R中处理NA的聚合函数

问题描述 投票:1回答:1

我试图使用聚合函数从csv文件中获取每日总和,但我遇到以下错误:

Error in Summary.factor(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), na.rm = FALSE) : ‘sum’ not meaningful for factors
Calls: aggregate ... aggregate.data.frame -> lapply -> FUN -> lapply ->          Summary.factor 
Execution halted

这是Data数据的链接

这是我的代码:

dat<-read.csv("Laoag_tc_induced.csv",header=TRUE,sep=",")
dat[dat == -999] <- NA
dat[dat == -888] <- 0
dat$Date <- as.Date(strptime(dat$key, '%Y_%m_%d_%H'))

df <- data.frame(dat$Date,dat$RR,dat$dist)
df <- aggregate(RR ~ Date, dat,sum)

names(df)[1] <- "Date"
names(df)[2] <- "Rain"

write.table(df,file="test.csv",sep=",")

我试过用:

df <- aggregate(RR ~ Date, dat,sum,na.rm=TRUE)

df <- aggregate(RR ~ Date,dat,sum,na.rm=TRUE,na.action=na.pass)

错误仍然是相同的:

‘sum’ not meaningful for factors
r aggregate na
1个回答
2
投票

'RR'中有某些元素,即" NA",将列的类更改为factor(也使用stringsAsFactors = FALSE)。选项是将na.strings中的NA字符串指定为NA

dat <- read.csv(file, header = TRUE, stringsAsFactors = FALSE, 
          na.strings = "   NA", strip.white = TRUE)

在进行OP的转换/替换后,

res <- aggregate(RR ~ Date, dat,sum)
head(res, 5)
#        Date  RR
#1 1994-08-09 0.0
#2 1994-08-10 0.0
#3 1994-08-11 0.0
#4 1994-08-12 0.3
#5 1994-08-13 0.0

由于OP表示日期正在变更,因此根据提供的数据工作正常

dat[78:81,]
#   X.1          key     SN CY     Lat.x    Lon.x     X   RR     Lat.y    Lon.y     dist       Date
#78  78  1994_8_19_0 199419 19 0.3700098 2.230531 49133 28.8 0.3176499 2.104727 824.8680 1994-08-19
#79  79  1994_8_19_6 199419 19 0.3787364 2.214823 49134 28.8 0.3176499 2.104727 765.4631 1994-08-19
#80  80 1994_8_19_12 199419 19 0.3857178 2.200860 49135 28.8 0.3176499 2.104727 720.0335 1994-08-19
#81  81 1994_8_19_18 199419 19 0.3926991 2.190388 49136 28.8 0.3176499 2.104727 700.1729 1994-08-19

与csv数据中的相同

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.