【发布时间】:2015-08-26 02:40:35
【问题描述】:
我正在编写一个函数来计算给定污染物的平均值。这是我的功能:
pollutantmean <- function(directory, pollutant, id = 1:332) {
files <- list.files(directory, full.names = TRUE) #creates a list of files
dat <- data.frame()
for (i in 1:332) {
data <- rbind(dat, read.csv(files[i])) #loops through the files and created a data frame
}
data_1 <- data[which(data[, "ID"] %in% id), ]
data_subset_n <- data_1[which(data_1[, "nitrate"] == "nitrate"), ]
data_subset_s <- data_1[which(data_1[, "sulfate"] == "sulfate"), ]
data_2 <- cbind(data_subset_s, data_subset_n) #binds the two pollutants
mean((data_2[ ,pollutant]), na.rm=TRUE) #calculates the mean of the pollutant
}
我已经经历了很多次,不知道会出现什么问题,但我的函数输出仍然是 NaN。
> pollutantmean("specdata", "sulfate", 1:10)
[1] NaN
请帮忙。
谢谢。
【问题讨论】:
-
查看post