函数输出NaN值(Linux)

时间:2015-06-10 17:23:54

标签: r function nan

我正在写一个函数来计算给定污染物的平均值。这是我的功能:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<a class="lead p-color learn-button togglebtn">
  <small>
    <span class="glyphicon glyphicon-plus">
      </span>&nbsp;<span id="toggleText">Learn More</span>
  </small>
</a>
</div>

<div id="myContent" class="row row-offset" style="margin: 0 30px">
  <div class="col-xs-6 col-md-4">
    <div class="lead caption text-center">
      <h3 class="h-color">Profile</h3>
    </div>
    <div class="thumbnail">
      <img style="height: 100px; width: auto;" class="img-circle" src="images/logo-bunny.png" alt="Thumbnail">
    </div>
    <div class="lead caption">
      <p class="p-color"><small>Some sample text. Some sample text.</small></p>
    </div>
  </div>
</div>

}

我经历了很多次并且不知道可能出现什么问题,但我的功能输出仍然是NaN。

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

请帮忙。

谢谢。

1 个答案:

答案 0 :(得分:0)

没关系,我找到了答案。我在for循环中将值赋给了&#39;数据&#39;而不是&#39; dat&#39;。