[R] R is converting arg input to scientific notation, which is bad!
David Winsemius
dwinsemius at comcast.net
Fri Jun 20 04:30:39 CEST 2014
On Jun 19, 2014, at 12:08 PM, efridge wrote:
> Hello,
>
> 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
>
> *The problem is that when a single number (30, 104, 223) is input, it's
> being stored as 3e+01 and no longer corresponds with the .csv file I'm
> trying to call (030.csv).*
>
> 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)
> }
>
> I'm curious if formatC() is converting the number to scientific notation or
> is that the default for R if there is no sequence when there could be.
I think it's your `digits = 0` argument:
> formatC(20, digits = 3, width = 3, flag = "0")
[1] "020"
>
> 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.
>
>
--
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list