[R] Receiving NaN message

Robert Piliero robert.piliero at gmail.com
Mon Jan 16 21:10:12 CET 2017


I am working on a Coursera assignment and have combined 332 files into a
single data frame called "dat". The dataframe has 4 columns,

1. Date
2. Sulfate (numerical values)
3. Nitrate  (numerical  values)
4. ID # (numerical values).

Our assignment is to write a function pollutantmean <- function(directory,
pollutant, ID). whereby we can calculate the mean by inputting the
pollutant name and ID #.

I have reached the stage of subsetting the date e.g. by ID # 1-10, however
when I do so and then calculate the mean of this subset I receive the NaN
message (even though I have instructed R to disregard the "NA"'s).

*Beginning Code: *
specdata <- ("C:/Users/rober/specdata")
files_full <- list.files(specdata, full.names=TRUE)
dat <- data.frame()
for (i in 1:332){
  dat <- rbind(dat,read.csv(files_full[i]))
mean(dat$sulfate, na.rm=TRUE)

*Code which generated the NaN message. *
dat1_10 <- dat[which(dat[,ID] ==1:10),]
mean(dat1_10$sulfate, na.rm=TRUE)

Am I making a mistake in subsetting the rows with ID's 1:10? Any advice
would be appreciated.

Thank you,


Robert J. Piliero

Cell: (617) 283 1020
38 Linnaean St. #6
Cambridge, MA, 02138

	[[alternative HTML version deleted]]

More information about the R-help mailing list