【问题标题】:Function output NaN value (Linux)函数输出 NaN 值(Linux)
【发布时间】: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

请帮忙。

谢谢。

【问题讨论】:

标签: r function nan


【解决方案1】:

没关系,我找到了答案。在 for 循环中,我将值分配给了 'data' 而不是 'dat'。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 2017-01-21
    • 2020-12-22
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多