Re: [R] R is converting arg input to scientific notation, which is bad!
The digits argument for formatC is for sigfigs. I found a solution within the formatC argument list. When I added the format arg to formatC: filenames <- paste0(formatC(id, digits = 0, width = 3, format = "d", flag = "0"), ".csv") ..it miraculously worked. d is the format value for integers. Hope someone else can learn from this! -- View this message in context: http://r.789695.n4.nabble.com/R-is-converting-arg-input-to-scientific-notation-which-is-bad-tp4692389p4692459.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R is converting arg input to scientific notation, which is bad!
On 20 Jun 2014, at 04:30 , David Winsemius wrote: > I think it's your `digits = 0` argument: > >> formatC(20, digits = 3, width = 3, flag = "0") > [1] "020" At any rate, sprintf() is often more convenient: > sprintf("%03d.csv", c(30, 104, 223)) [1] "030.csv" "104.csv" "223.csv" -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R is converting arg input to scientific notation, which is bad!
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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] R is converting arg input to scientific notation, which is bad!
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. 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. -- View this message in context: http://r.789695.n4.nabble.com/R-is-converting-arg-input-to-scientific-notation-which-is-bad-tp4692389.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.