2
votes

Firstly, real new to R here.

I have a function intended to evaluate the values in columns spread over many tables. I have an argument in the function that allows the user to input what sequence of tables they want to draw data from. The function seems to work fine, but when the user inputs a single number (over 9) instead of a sequence using the : operator, I find an error message:

the input for this example is 30

Error in file(file, "rt") : cannot open the connection

5 file(file, "rt")

4 read.table(file = file, header = header, sep = sep, quote = quote, dec = dec, fill = fill, comment.char = comment.char, ...)

3 FUN("specdata/3e+01.csv"[[1L]], ...)

2 lapply(filepaths, read.csv)

1 pollutantmean("specdata", "nitrate", 30)

In addition: Warning message:

In file(file, "rt") : cannot open file 'specdata/3e+01.csv': No such file or directory

Below is my code:

pollutantmean <- function(directory, pollutant, id = 1:332){
filenames <- paste0(formatC(id, digits = 0, width = 3, flag = "0"), ".csv")
filepaths <- file.path(directory, filenames)
list_of_data_frames <- lapply(filepaths, read.csv)
big.df<-do.call(rbind,list_of_data_frames)
print(str(big.df))
mean(big.df[,pollutant], na.rm=TRUE)
}

Many of you probably recognize this as a coursera assignment. It's beyond the due date and I've submitted some (pretty not good) code for it. I just want to have a good understanding of what's going on here, as this type of work is directly related to my research.

1
I think it's converting the input to scientific notation when the input isn't a sequence. This is going to be a headache, as the input is being used to index the tables.efridge

1 Answers

4
votes

You want to make sure you format your values as integers as well

formatC(id,digits=0, width=3, flag="0", format="d")

or you could use

sprintf("%03d", id)

inside your paste statement. Both of those should prevent the scientific notation.