Monday, 15 September 2014

csv - Using formatC to add 0's to sequence in arg returning error in R -



csv - Using formatC to add 0's to sequence in arg returning error in R -

firstly, real new r here.

i have function intended evaluate values in columns spread on many tables. have argument in function allows user input sequence of tables want draw info from. function seems work fine, when user inputs single number (over 9) instead of sequence using : operator, find error message:

the input illustration 30

error in file(file, "rt") : cannot open 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 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 recognize coursera assignment. it's beyond due date , i've submitted (pretty not good) code it. want have understanding of what's going on here, type of work straight related research.

you want create sure format values integers well

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

or use

sprintf("%03d", id)

inside paste statement. both of should prevent scientific notation.

r csv formatting

No comments:

Post a Comment