Re: [R] Error in reading subset of data from CSV file
I suppose you could try quote=TRUE Jim On Sun, Sep 4, 2016 at 8:13 PM, Christofer Bogasowrote: > Thanks Jim. But my data is like that and I have to live with that. Any > idea on workaround. Thanks, > > On Sun, Sep 4, 2016 at 3:40 PM, Jim Lemon wrote: >> Hi Christofer, >> You have embedded commas in your data structure. This is guaranteed to >> mess up a CSV read. >> >> Jim >> >> >> On Sun, Sep 4, 2016 at 5:54 PM, Christofer Bogaso >> wrote: >>> Hi again, >>> >>> I was trying to read a subset of Data from a CSV file using below code >>> as example : >>> >>> library(sqldf) >>> >>> Dat = structure(list(`col 1` = c(133261L, 133261L, 133262L, 133262L >>> ), `col 2` = structure(1:4, .Label = c("aaa1", "aaa2", "bbb1, bbb", >>> "bbb3, bbb"), class = "factor"), `col 3` = c(10.59, 10.56, 10.59, >>> 10.56), `col 4` = c(10.59, 10.56, 10.59, 10.56), `col 5` = c(10.59, >>> 10.56, 10.59, 10.56), `col 6` = structure(c(1L, 2L, 1L, 2L), .Label = >>> c("04-Jul-16", >>> "05-Jul-16"), class = "factor")), .Names = c("col 1", "col 2", >>> "col 3", "col 4", "col 5", "col 6"), class = "data.frame", row.names = c(NA, >>> -4L)) >>> Dat >>> >>> write.csv(Dat, "Dat.csv", quote = FALSE, row.names = FALSE) >>> >>> ReadName = '133261' >>> read.csv.sql("Dat.csv", sql = "select * from file where 'col 1' = ReadName") >>> >>> Loading required package: tcltk >>> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, >>> : >>> line 1 did not have 7 elements >>> >>> This code generates above Error. Could you please help me with a >>> pointer where something went wrong? My actual CSV file is quite huge >>> so I cant read it as whole. However basic structure of my original >>> file is similar as above "Dat" >>> >>> Thanks for your time. >>> >>> __ >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Creating a loop with code from the mblm package
Please go through an R tutorial or two before posting further here. There are many good ones on the web. Some recommendations can be found here: https://www.rstudio.com/online-learning/ Your question: "And my Year column is the first column in my csv file, which I thought made it column 0. Am I mistaken, is it supposed to be column 1?" is absolutely basic, and indicates that you have not yet made much effort to learn R. The inevitable result, of course, is confusion and errors of the sort you describe. The answer is that R indices start at 1, but there is a great deal more to them than that, which tutorials would tell you about. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Sep 4, 2016 at 7:10 AM, Bailey Hewittwrote: > Hi Jim, > > > Thank you for the suggestion. Unfortunately, when I tried this it gave me the > same error as I was getting before. I was wondering the same thing as you, > because of the way my function is set up is it even possible to iterate > through columns in that type of function? And my Year column is the first > column in my csv file, which I thought made it column 0. Am I mistaken, is it > supposed to be column 1? > > > Thanks! > > > Bailey > > > > From: Jim Lemon > Sent: September 4, 2016 4:55 AM > To: Bailey Hewitt > Cc: R-help > Subject: Re: [R] Creating a loop with code from the mblm package > > Hi Bailey, > Treat it as a guess, but try this: > > for (i in c(1:3)){ > y<-mydata[,i] > x <- mblm(y ~ Year, mydata, repeated = FALSE) > print(x) > } > > I'm not sure that you can mix indexed columns with column names. Also, > Year is column 4, no? > > Jim > > > On Sun, Sep 4, 2016 at 11:43 AM, Bailey Hewitt wrote: >> Hello, >> >> >> I am a novice in coding in R and have come across an error I am having a >> hard time fixing. I am trying to use the mblm package to run a Theil-Sen >> linear model. The code for this function is: >> >> mblm(Y ~ X, dataframe, repeated = FALSE) >> >> My goal is to put this into a loop so that I can calculate the Theil-Sen >> slope of each column in my csv. file. The file contains one column of years >> (x value) and 3 columns of days of the year (y values). All columns are the >> same length. The code I currently have is: >> >> >> read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, >> sep = ",") >> >> mydata= read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = >> TRUE, sep = ",") >> >> >> attach(mydata) >> >> >> install.packages("mblm") >> >> library("mblm") >> >> >> for (i in c(1:3)){ >> >> x <- mblm(mydata[,i] ~ Year, mydata, repeated = FALSE) >> >> print(x) >> >> } >> >> >> Which gives me the following error: >> >> Error in names(res$residuals) = as.character(1:length(res$residuals)) : >> >> 'names' attribute [2] must be the same length as the vector [0] >> >> >> Which I cannot seem to solve although as I understand it it is an error that >> I am causing in the mblm package. If anyone has any insight into how I >> [[elided Hotmail spam]] >> >> >> Bailey >> >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help > > z.ch/mailman/listinfo/r-help> > stat.ethz.ch > The main R mailing list, for announcements about the development of R and the > availability of new code, questions and answers about problems and solutions > using R ... > > > >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Problem with adding a row in a data table
The "c" function creates vectors. Rows of data frames are data frames, not vectors. new_row <- data.frame( Prod_name = "Day_name", `Date 1`=1, `Date 2`=2,`Date 3`=3 ) data_may <- rbind( new_row, data_may ) Furthermore, data frames are NOT spreadsheets. "Day_num" looks suspiciously UNlike a product name, which may mean the corresponding values in that row are not Dates, which would also lead you into trouble. Please read the Posting Guide. In particular, you should read about making your examples reproducible. Part of that is posting in plain text and using the dput function to give us your sample data, because all too often the problem lies in the details of how you have imported and manipulated your data and the shortest way for us to see that the data are okay is to see it as it exists in your R script so far. -- Sent from my phone. Please excuse my brevity. On September 4, 2016 6:22:48 AM PDT, Filippos Katsioswrote: >Dear All, > >I am relatively new to R and certainly new to the e-mailing list. I >need >your help. I am working on a data frame, which looks like this: > >Prod_name | Date 1 | Date 2 | Date 3 | >--|-||--| >Product 1| 3 | 4 | 0 | >--|-||--| >Product 2| 5 | 3 | 3 | >--|-||--| >Product 3| 2 | 8 | 5 | > >I am trying to add a new row with the following results: > >Prod_name | Date 1 | Date 2 | Date 3 | >--|-||--| >Day_num| 1 | 2 | 3 | >--|-||--| >Product 1| 3 | 4 | 0 | >--|-||--| >Product 2| 5 | 3 | 3 | >--|-||--| >Product 3| 2 | 8 | 5 | > >Bellow you can find the things I tried and the results. >1) >r <- 1 >newrow <- rep(1:7, 5, len=ncol(data_may)-1) >insertRow <- function(data_may, newrow, r) { >data_may[seq(r+1,nrow(data_may)+1),] <- >data_may[seq(r,nrow(data_may)),] > data_may[r,] <- newrow > data_may >} > >It doesn't put the new row. >2) >data_may<-rbind(data_may,c("Day_num",newrow)) > >Error: cannot convert object to a data frame > >3) >data_may[2093,]<-c("Day_num",rep(1:7, 5, len=ncol(data_may)-1)) > >It makes all the columns characters and when i try to change it it says >that you can change a list > >How can I add the row while keeping the columns (apart from the first >one) >as numeric or double or integer? > >Thank you, in advance, for your help! > >Kind regards >Filippos > > [[alternative HTML version deleted]] > >__ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Problem with adding a row in a data table
Dear All, I am relatively new to R and certainly new to the e-mailing list. I need your help. I am working on a data frame, which looks like this: Prod_name | Date 1 | Date 2 | Date 3 | --|-||--| Product 1| 3 | 4 | 0 | --|-||--| Product 2| 5 | 3 | 3 | --|-||--| Product 3| 2 | 8 | 5 | I am trying to add a new row with the following results: Prod_name | Date 1 | Date 2 | Date 3 | --|-||--| Day_num| 1 | 2 | 3 | --|-||--| Product 1| 3 | 4 | 0 | --|-||--| Product 2| 5 | 3 | 3 | --|-||--| Product 3| 2 | 8 | 5 | Bellow you can find the things I tried and the results. 1) r <- 1 newrow <- rep(1:7, 5, len=ncol(data_may)-1) insertRow <- function(data_may, newrow, r) { data_may[seq(r+1,nrow(data_may)+1),] <- data_may[seq(r,nrow(data_may)),] data_may[r,] <- newrow data_may } It doesn't put the new row. 2) data_may<-rbind(data_may,c("Day_num",newrow)) Error: cannot convert object to a data frame 3) data_may[2093,]<-c("Day_num",rep(1:7, 5, len=ncol(data_may)-1)) It makes all the columns characters and when i try to change it it says that you can change a list How can I add the row while keeping the columns (apart from the first one) as numeric or double or integer? Thank you, in advance, for your help! Kind regards Filippos [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Problem with adding a row in a data table
Please use Reply-all to keep the mailing list in the loop. I cannot provide private assistance, and others may provide valuable input or respond faster than I can. It is very common that people cannot provide the original data. That means more work for YOU, though, not for us. It is up to you to create a small simulated data set and process it as if it were your original data. Your idea will indeed be a good algorithm, but you will fail in R if you don't set it up differently. Read [1] and provide us with a reproducible example data set and desired result and someone here will be able to show you how to do it correctly. [1] http://adv-r.had.co.nz/Reproducibility.html -- Sent from my phone. Please excuse my brevity. On September 4, 2016 8:28:39 AM PDT, Filippos Katsioswrote: >Dear Jeff, >I am sorry but I am not allowed to share the original data. You are >right >about the Prod_name row. However, my goal is to split the columns >"Date 1" >etc into weekdays and weekends and manipulate them separately. I >thought >this would be the best way to do that (Assign to each day a number from >1:7 >and then splitting them by a logical vector). Thank you for your help >and >your time! > >Filippos > >On 4 September 2016 at 18:20, Jeff Newmiller >wrote: > >> The "c" function creates vectors. Rows of data frames are data >frames, not >> vectors. >> >> new_row <- data.frame( Prod_name = "Day_name", `Date 1`=1, `Date >> 2`=2,`Date 3`=3 ) >> data_may <- rbind( new_row, data_may ) >> >> Furthermore, data frames are NOT spreadsheets. "Day_num" looks >> suspiciously UNlike a product name, which may mean the corresponding >values >> in that row are not Dates, which would also lead you into trouble. >> >> Please read the Posting Guide. In particular, you should read about >making >> your examples reproducible. Part of that is posting in plain text and >using >> the dput function to give us your sample data, because all too often >the >> problem lies in the details of how you have imported and manipulated >your >> data and the shortest way for us to see that the data are okay is to >see it >> as it exists in your R script so far. >> -- >> Sent from my phone. Please excuse my brevity. >> >> On September 4, 2016 6:22:48 AM PDT, Filippos Katsios > >> wrote: >> >Dear All, >> > >> >I am relatively new to R and certainly new to the e-mailing list. I >> >need >> >your help. I am working on a data frame, which looks like this: >> > >> >Prod_name | Date 1 | Date 2 | Date 3 | >> >--|-||--| >> >Product 1| 3 | 4 | 0 | >> >--|-||--| >> >Product 2| 5 | 3 | 3 | >> >--|-||--| >> >Product 3| 2 | 8 | 5 | >> > >> >I am trying to add a new row with the following results: >> > >> >Prod_name | Date 1 | Date 2 | Date 3 | >> >--|-||--| >> >Day_num| 1 | 2 | 3 | >> >--|-||--| >> >Product 1| 3 | 4 | 0 | >> >--|-||--| >> >Product 2| 5 | 3 | 3 | >> >--|-||--| >> >Product 3| 2 | 8 | 5 | >> > >> >Bellow you can find the things I tried and the results. >> >1) >> >r <- 1 >> >newrow <- rep(1:7, 5, len=ncol(data_may)-1) >> >insertRow <- function(data_may, newrow, r) { >> >data_may[seq(r+1,nrow(data_may)+1),] <- >> >data_may[seq(r,nrow(data_may)),] >> > data_may[r,] <- newrow >> > data_may >> >} >> > >> >It doesn't put the new row. >> >2) >> >data_may<-rbind(data_may,c("Day_num",newrow)) >> > >> >Error: cannot convert object to a data frame >> > >> >3) >> >data_may[2093,]<-c("Day_num",rep(1:7, 5, len=ncol(data_may)-1)) >> > >> >It makes all the columns characters and when i try to change it it >says >> >that you can change a list >> > >> >How can I add the row while keeping the columns (apart from the >first >> >one) >> >as numeric or double or integer? >> > >> >Thank you, in advance, for your help! >> > >> >Kind regards >> >Filippos >> > >> > [[alternative HTML version deleted]] >> > >> >__ >> >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> >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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read
[R] Splines
Hi, I would like to use the C spline functions if R for a FORTRAN subroutine. What header file should I refer to? Regards Filippo Monari __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Error in reading subset of data from CSV file
Didnt work getting unused argument error. On Sun, Sep 4, 2016 at 4:47 PM, Jim Lemonwrote: > I suppose you could try quote=TRUE > > Jim > > > On Sun, Sep 4, 2016 at 8:13 PM, Christofer Bogaso > wrote: >> Thanks Jim. But my data is like that and I have to live with that. Any >> idea on workaround. Thanks, >> >> On Sun, Sep 4, 2016 at 3:40 PM, Jim Lemon wrote: >>> Hi Christofer, >>> You have embedded commas in your data structure. This is guaranteed to >>> mess up a CSV read. >>> >>> Jim >>> >>> >>> On Sun, Sep 4, 2016 at 5:54 PM, Christofer Bogaso >>> wrote: Hi again, I was trying to read a subset of Data from a CSV file using below code as example : library(sqldf) Dat = structure(list(`col 1` = c(133261L, 133261L, 133262L, 133262L ), `col 2` = structure(1:4, .Label = c("aaa1", "aaa2", "bbb1, bbb", "bbb3, bbb"), class = "factor"), `col 3` = c(10.59, 10.56, 10.59, 10.56), `col 4` = c(10.59, 10.56, 10.59, 10.56), `col 5` = c(10.59, 10.56, 10.59, 10.56), `col 6` = structure(c(1L, 2L, 1L, 2L), .Label = c("04-Jul-16", "05-Jul-16"), class = "factor")), .Names = c("col 1", "col 2", "col 3", "col 4", "col 5", "col 6"), class = "data.frame", row.names = c(NA, -4L)) Dat write.csv(Dat, "Dat.csv", quote = FALSE, row.names = FALSE) ReadName = '133261' read.csv.sql("Dat.csv", sql = "select * from file where 'col 1' = ReadName") Loading required package: tcltk Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 1 did not have 7 elements This code generates above Error. Could you please help me with a pointer where something went wrong? My actual CSV file is quite huge so I cant read it as whole. However basic structure of my original file is similar as above "Dat" Thanks for your time. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Creating a loop with code from the mblm package
Hi Bailey, Treat it as a guess, but try this: for (i in c(1:3)){ y<-mydata[,i] x <- mblm(y ~ Year, mydata, repeated = FALSE) print(x) } I'm not sure that you can mix indexed columns with column names. Also, Year is column 4, no? Jim On Sun, Sep 4, 2016 at 11:43 AM, Bailey Hewittwrote: > Hello, > > > I am a novice in coding in R and have come across an error I am having a hard > time fixing. I am trying to use the mblm package to run a Theil-Sen linear > model. The code for this function is: > > mblm(Y ~ X, dataframe, repeated = FALSE) > > My goal is to put this into a loop so that I can calculate the Theil-Sen > slope of each column in my csv. file. The file contains one column of years > (x value) and 3 columns of days of the year (y values). All columns are the > same length. The code I currently have is: > > > read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, > sep = ",") > > mydata= read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = > TRUE, sep = ",") > > > attach(mydata) > > > install.packages("mblm") > > library("mblm") > > > for (i in c(1:3)){ > > x <- mblm(mydata[,i] ~ Year, mydata, repeated = FALSE) > > print(x) > > } > > > Which gives me the following error: > > Error in names(res$residuals) = as.character(1:length(res$residuals)) : > > 'names' attribute [2] must be the same length as the vector [0] > > > Which I cannot seem to solve although as I understand it it is an error that > I am causing in the mblm package. If anyone has any insight into how I could > start fixing this that would be greatly appreciated! > > > Bailey > > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] impossible # of errors in a simple code
A useful rule is to fix the first error you understand and hope that the others go away. On 04/09/2016 04:05, Tamar Michaeli wrote: Any help in resolving the following errors will be appreciated: pollutantmean <- function(directory, pollutant, id=1:332) + file_inc <- list.files("specdata", full.names=TRUE) So what did you hope your function was going to do, if you called it? dat <- data.frame() for(i in 1:10) { + dat <- rbind(dat, read.csv(file_inc[i])) + } Error in read.table(file = file, header = header, sep = sep, quote = quote, : object 'file_inc' not found Why did you think at this point that you had an object called file_inc? sulfate <- subset(dat( , 2)) Error in subset(dat(, 2)) : could not find function "dat" So you did not manage anywhere to define a function called dat. nitrate <- subset(dat( , 3)) Error in subset(dat(, 3)) : could not find function "dat" mean(pollutant) Error in mean(pollutant) : object 'pollutant' not found You have never defined it. save"pollutantmean.R" Error: unexpected string constant in "save"pollutantmean.R"" ?save might help you here. [[alternative HTML version deleted]] Please do not post in HTML as it makes your post unreadable. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. -- Michael http://www.dewey.myzen.co.uk/home.html __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Error in reading subset of data from CSV file
Shouldn't get that with write.csv. Jim On Sun, Sep 4, 2016 at 9:29 PM, Christofer Bogasowrote: > Didnt work getting unused argument error. > > On Sun, Sep 4, 2016 at 4:47 PM, Jim Lemon wrote: >> I suppose you could try quote=TRUE >> >> Jim >> >> >> On Sun, Sep 4, 2016 at 8:13 PM, Christofer Bogaso >> wrote: >>> Thanks Jim. But my data is like that and I have to live with that. Any >>> idea on workaround. Thanks, >>> >>> On Sun, Sep 4, 2016 at 3:40 PM, Jim Lemon wrote: Hi Christofer, You have embedded commas in your data structure. This is guaranteed to mess up a CSV read. Jim On Sun, Sep 4, 2016 at 5:54 PM, Christofer Bogaso wrote: > Hi again, > > I was trying to read a subset of Data from a CSV file using below code > as example : > > library(sqldf) > > Dat = structure(list(`col 1` = c(133261L, 133261L, 133262L, 133262L > ), `col 2` = structure(1:4, .Label = c("aaa1", "aaa2", "bbb1, bbb", > "bbb3, bbb"), class = "factor"), `col 3` = c(10.59, 10.56, 10.59, > 10.56), `col 4` = c(10.59, 10.56, 10.59, 10.56), `col 5` = c(10.59, > 10.56, 10.59, 10.56), `col 6` = structure(c(1L, 2L, 1L, 2L), .Label = > c("04-Jul-16", > "05-Jul-16"), class = "factor")), .Names = c("col 1", "col 2", > "col 3", "col 4", "col 5", "col 6"), class = "data.frame", row.names = > c(NA, > -4L)) > Dat > > write.csv(Dat, "Dat.csv", quote = FALSE, row.names = FALSE) > > ReadName = '133261' > read.csv.sql("Dat.csv", sql = "select * from file where 'col 1' = > ReadName") > > Loading required package: tcltk > Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, > na.strings, : > line 1 did not have 7 elements > > This code generates above Error. Could you please help me with a > pointer where something went wrong? My actual CSV file is quite huge > so I cant read it as whole. However basic structure of my original > file is similar as above "Dat" > > Thanks for your time. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Need advice on linear programming in R
Hi Michael, On top of all suggestions, I can mention the following packages for linear programming problems: https://cran.r-project.org/web/packages/linprog/linprog.pdf https://cran.r-project.org/web/packages/lpSolve/lpSolve.pdf https://cran.r-project.org/web/packages/clue/clue.pdf (see clue package solve_LSAP function) https://cran.r-project.org/web/packages/adagio/adagio.pdf As a reference book, "Using R for Numerical Analysis in Science and Engineering", CRC press: https://www.crcpress.com/Using-R-for-Numerical-Analysis-in-Science-and-Engineering/Bloomfield/p/book/9781439884485 Further, I share with you the code implementing a greedy solution to your assignment problem: set.seed(1023) nbin <- 5 nrow <- 100 nvalue <- nbin*nrow # generating random values gMat2 <- matrix(as.integer(Mod(rnorm(nvalue, 5, 10))), nrow = nrow) gMat2 # since you want to maximize the minimum bin sums value # let us already find the maximum value per row and store its corresponding # column id inside rowmax rowmax <- apply(gMat2, 1, function(x) {which.max(x)}) rowmax # maximum value per each row max_per_row <- sapply(1:nrow, function(x) gMat2[x, rowmax[x]]) names(max_per_row) <- as.character(1:nrow) max_per_row # sorting max_per_row values in descreasing order sorted <- base::sort(max_per_row, decreasing=TRUE) sorted # bin where to store sums bin <- vector(mode="numeric", length = nbin) # assignment output based on sorted vector output <- c() # looping on sorted and assign the bin with minimum current sum for (i in seq_along(sorted)) { s <- sorted[i] min.bin <- which.min(bin) bin[min.bin] <- bin[min.bin] + s output <- c(output, min.bin) } df <- cbind(sorted, output) ord <- order(as.integer(rownames(df))) df2 <- df[ord,] colnames(df2) <- c("value", "selected_bin") df2 <- data.frame(selected_column = rowmax, df2) # assignments table df2 # resulting bins sum bin - Best, GG [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Error in reading subset of data from CSV file
Hi again, I was trying to read a subset of Data from a CSV file using below code as example : library(sqldf) Dat = structure(list(`col 1` = c(133261L, 133261L, 133262L, 133262L ), `col 2` = structure(1:4, .Label = c("aaa1", "aaa2", "bbb1, bbb", "bbb3, bbb"), class = "factor"), `col 3` = c(10.59, 10.56, 10.59, 10.56), `col 4` = c(10.59, 10.56, 10.59, 10.56), `col 5` = c(10.59, 10.56, 10.59, 10.56), `col 6` = structure(c(1L, 2L, 1L, 2L), .Label = c("04-Jul-16", "05-Jul-16"), class = "factor")), .Names = c("col 1", "col 2", "col 3", "col 4", "col 5", "col 6"), class = "data.frame", row.names = c(NA, -4L)) Dat write.csv(Dat, "Dat.csv", quote = FALSE, row.names = FALSE) ReadName = '133261' read.csv.sql("Dat.csv", sql = "select * from file where 'col 1' = ReadName") Loading required package: tcltk Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 1 did not have 7 elements This code generates above Error. Could you please help me with a pointer where something went wrong? My actual CSV file is quite huge so I cant read it as whole. However basic structure of my original file is similar as above "Dat" Thanks for your time. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Error in reading subset of data from CSV file
Hi Christofer, You have embedded commas in your data structure. This is guaranteed to mess up a CSV read. Jim On Sun, Sep 4, 2016 at 5:54 PM, Christofer Bogasowrote: > Hi again, > > I was trying to read a subset of Data from a CSV file using below code > as example : > > library(sqldf) > > Dat = structure(list(`col 1` = c(133261L, 133261L, 133262L, 133262L > ), `col 2` = structure(1:4, .Label = c("aaa1", "aaa2", "bbb1, bbb", > "bbb3, bbb"), class = "factor"), `col 3` = c(10.59, 10.56, 10.59, > 10.56), `col 4` = c(10.59, 10.56, 10.59, 10.56), `col 5` = c(10.59, > 10.56, 10.59, 10.56), `col 6` = structure(c(1L, 2L, 1L, 2L), .Label = > c("04-Jul-16", > "05-Jul-16"), class = "factor")), .Names = c("col 1", "col 2", > "col 3", "col 4", "col 5", "col 6"), class = "data.frame", row.names = c(NA, > -4L)) > Dat > > write.csv(Dat, "Dat.csv", quote = FALSE, row.names = FALSE) > > ReadName = '133261' > read.csv.sql("Dat.csv", sql = "select * from file where 'col 1' = ReadName") > > Loading required package: tcltk > Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : > line 1 did not have 7 elements > > This code generates above Error. Could you please help me with a > pointer where something went wrong? My actual CSV file is quite huge > so I cant read it as whole. However basic structure of my original > file is similar as above "Dat" > > Thanks for your time. > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Error in reading subset of data from CSV file
Thanks Jim. But my data is like that and I have to live with that. Any idea on workaround. Thanks, On Sun, Sep 4, 2016 at 3:40 PM, Jim Lemonwrote: > Hi Christofer, > You have embedded commas in your data structure. This is guaranteed to > mess up a CSV read. > > Jim > > > On Sun, Sep 4, 2016 at 5:54 PM, Christofer Bogaso > wrote: >> Hi again, >> >> I was trying to read a subset of Data from a CSV file using below code >> as example : >> >> library(sqldf) >> >> Dat = structure(list(`col 1` = c(133261L, 133261L, 133262L, 133262L >> ), `col 2` = structure(1:4, .Label = c("aaa1", "aaa2", "bbb1, bbb", >> "bbb3, bbb"), class = "factor"), `col 3` = c(10.59, 10.56, 10.59, >> 10.56), `col 4` = c(10.59, 10.56, 10.59, 10.56), `col 5` = c(10.59, >> 10.56, 10.59, 10.56), `col 6` = structure(c(1L, 2L, 1L, 2L), .Label = >> c("04-Jul-16", >> "05-Jul-16"), class = "factor")), .Names = c("col 1", "col 2", >> "col 3", "col 4", "col 5", "col 6"), class = "data.frame", row.names = c(NA, >> -4L)) >> Dat >> >> write.csv(Dat, "Dat.csv", quote = FALSE, row.names = FALSE) >> >> ReadName = '133261' >> read.csv.sql("Dat.csv", sql = "select * from file where 'col 1' = ReadName") >> >> Loading required package: tcltk >> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : >> line 1 did not have 7 elements >> >> This code generates above Error. Could you please help me with a >> pointer where something went wrong? My actual CSV file is quite huge >> so I cant read it as whole. However basic structure of my original >> file is similar as above "Dat" >> >> Thanks for your time. >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] impossible # of errors in a simple code
Any help in resolving the following errors will be appreciated: > pollutantmean <- function(directory, pollutant, id=1:332) + file_inc <- list.files("specdata", full.names=TRUE) > dat <- data.frame() > for(i in 1:10) { + dat <- rbind(dat, read.csv(file_inc[i])) + } Error in read.table(file = file, header = header, sep = sep, quote = quote, : object 'file_inc' not found > sulfate <- subset(dat( , 2)) Error in subset(dat(, 2)) : could not find function "dat" > nitrate <- subset(dat( , 3)) Error in subset(dat(, 3)) : could not find function "dat" > mean(pollutant) Error in mean(pollutant) : object 'pollutant' not found > save"pollutantmean.R" Error: unexpected string constant in "save"pollutantmean.R"" [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Problem with adding a row in a data table
Dear all, I believe that this will be a more helpful way to put the problem: structure(list(Prod_name = c("Banana", "Apple", "Orange", "Yoghurt", "Eggs", "Milk", "Day_num"), X1.1.2000 = c("1", "0", "4", "3", "6", "2", "1"), X2.1.2000 = c("2", "4", "1", "5", "3", "0", "2" ), X3.1.2000 = c("1", "5", "2", "3", "0", "4", "3"), X4.1.2000 = c("2", "4", "4", "1", "0", "0", "4"), X5.1.2000 = c("0", "0", "1", "0", "2", "3", "5"), X6.1.2000 = c("1", "3", "2", "1", "4", "1", "6" ), X7.1.2000 = c("5", "4", "5", "2", "2", "1", "7")), .Names = c("Prod_name", "X1.1.2000", "X2.1.2000", "X3.1.2000", "X4.1.2000", "X5.1.2000", "X6.1.2000", "X7.1.2000"), row.names = c(NA, 7L), class = "data.frame") and the code: https://gist.github.com/anonymous/750b02ad5db448d45c92a79059bf9844 Thank you for your help Filippos On 4 September 2016 at 19:30, Jeff Newmillerwrote: > Please use Reply-all to keep the mailing list in the loop. I cannot > provide private assistance, and others may provide valuable input or > respond faster than I can. > > It is very common that people cannot provide the original data. That means > more work for YOU, though, not for us. It is up to you to create a small > simulated data set and process it as if it were your original data. > > Your idea will indeed be a good algorithm, but you will fail in R if you > don't set it up differently. Read [1] and provide us with a reproducible > example data set and desired result and someone here will be able to show > you how to do it correctly. > > [1] http://adv-r.had.co.nz/Reproducibility.html > -- > Sent from my phone. Please excuse my brevity. > > On September 4, 2016 8:28:39 AM PDT, Filippos Katsios > wrote: > >Dear Jeff, > >I am sorry but I am not allowed to share the original data. You are > >right > >about the Prod_name row. However, my goal is to split the columns > >"Date 1" > >etc into weekdays and weekends and manipulate them separately. I > >thought > >this would be the best way to do that (Assign to each day a number from > >1:7 > >and then splitting them by a logical vector). Thank you for your help > >and > >your time! > > > >Filippos > > > >On 4 September 2016 at 18:20, Jeff Newmiller > >wrote: > > > >> The "c" function creates vectors. Rows of data frames are data > >frames, not > >> vectors. > >> > >> new_row <- data.frame( Prod_name = "Day_name", `Date 1`=1, `Date > >> 2`=2,`Date 3`=3 ) > >> data_may <- rbind( new_row, data_may ) > >> > >> Furthermore, data frames are NOT spreadsheets. "Day_num" looks > >> suspiciously UNlike a product name, which may mean the corresponding > >values > >> in that row are not Dates, which would also lead you into trouble. > >> > >> Please read the Posting Guide. In particular, you should read about > >making > >> your examples reproducible. Part of that is posting in plain text and > >using > >> the dput function to give us your sample data, because all too often > >the > >> problem lies in the details of how you have imported and manipulated > >your > >> data and the shortest way for us to see that the data are okay is to > >see it > >> as it exists in your R script so far. > >> -- > >> Sent from my phone. Please excuse my brevity. > >> > >> On September 4, 2016 6:22:48 AM PDT, Filippos Katsios > > > >> wrote: > >> >Dear All, > >> > > >> >I am relatively new to R and certainly new to the e-mailing list. I > >> >need > >> >your help. I am working on a data frame, which looks like this: > >> > > >> >Prod_name | Date 1 | Date 2 | Date 3 | > >> >--|-||--| > >> >Product 1| 3 | 4 | 0 | > >> >--|-||--| > >> >Product 2| 5 | 3 | 3 | > >> >--|-||--| > >> >Product 3| 2 | 8 | 5 | > >> > > >> >I am trying to add a new row with the following results: > >> > > >> >Prod_name | Date 1 | Date 2 | Date 3 | > >> >--|-||--| > >> >Day_num| 1 | 2 | 3 | > >> >--|-||--| > >> >Product 1| 3 | 4 | 0 | > >> >--|-||--| > >> >Product 2| 5 | 3 | 3 | > >> >--|-||--| > >> >Product 3| 2 | 8 | 5 | > >> > > >> >Bellow you can find the things I tried and the results. > >> >1) > >> >r <- 1 > >> >newrow <- rep(1:7, 5, len=ncol(data_may)-1) > >> >insertRow <- function(data_may, newrow, r) { > >> >data_may[seq(r+1,nrow(data_may)+1),] <- > >> >data_may[seq(r,nrow(data_may)),] > >> > data_may[r,] <- newrow > >> > data_may > >> >} > >> > > >> >It doesn't put the new
Re: [R] Problem with adding a row in a data table
My suggested approach: dta <- structure(list(Prod_name = c("Banana", "Apple", "Orange", "Yoghurt", "Eggs", "Milk", "Day_num"), X1.1.2000 = c("1", "0", "4", "3", "6", "2", "1"), X2.1.2000 = c("2", "4", "1", "5", "3", "0", "2" ), X3.1.2000 = c("1", "5", "2", "3", "0", "4", "3"), X4.1.2000 = c("2", "4", "4", "1", "0", "0", "4"), X5.1.2000 = c("0", "0", "1", "0", "2", "3", "5"), X6.1.2000 = c("1", "3", "2", "1", "4", "1", "6" ), X7.1.2000 = c("5", "4", "5", "2", "2", "1", "7")), .Names = c("Prod_name", "X1.1.2000", "X2.1.2000", "X3.1.2000", "X4.1.2000", "X5.1.2000", "X6.1.2000", "X7.1.2000"), row.names = c(NA, 7L), class = "data.frame") # The Day_num values ARE NOT data you will be aggregating and # should not be in the data frame with meaningful values. dta <- dta[ 1:6, ] # forget last garbage line # assuming your data are intended to be numeric for( i in 2:8 ) { dta[[ i ]] <- as.numeric( dta[[ i ]] ) } # you didn't say what computation you want to accomplish on the data # assuming you want to add values up by product and part of week # base R functions # generally useful to set timezone when using POSIXt types Sys.setenv( TZ="Etc/GMT" ) # gather data values from multiple columns into long form # I find this function very confusing, but it does work if you # don't like depending on contributed packages that are easier to # understand dtaLong <- reshape( dta , idvar = "Prod_name" , varying = 1+seq.int( length( dta ) - 1 ) , v.names = "value" , timevar = "XDates" , times = names( dta )[ 1+seq.int( length( dta ) - 1 ) ] , direction = "long" ) # extract Date values from column names dtaLong$Dates <- as.Date( dtaLong$XDates, format="X%d.%m.%Y" ) # read about POSIX types in the help page ?DateTimeClasses dt_lt <- as.POSIXlt( dtaLong$Dates ) # extract the weekday information from the POSIXlt dtaLong$wday <- dt_lt$wday # Sunday==0 # identify rows corresponding to time of week dtaLong$WkPart <- ifelse( dtaLong$wday %in% c( 0, 6 ) , "Weekend" , "Weekday" ) # aggregate by sum the value grouping by Prod_name and WkPart dtaAgg <- aggregate( dtaLong$value , dtaLong[ , c( "Prod_name", "WkPart" ), drop=FALSE ] , FUN=sum ) # or using dplyr/tidyr library(dplyr) library(tidyr) library(lubridate) # "pipe" data frames from one step to the next dtaAgg2.a <- ( dta # tidyr way of making long form data %>% gather( XDates, value, -Prod_name ) ) # dtaAgg2.a is purely for studying what is happening dtaAgg2.b <- ( dta # tidyr way of making long form data %>% gather( XDates, value, -Prod_name ) %>% mutate( Dates = as.Date( XDates, format="X%d.%m.%Y" ) , WkPart = ifelse( wday( Dates ) %in% c( 0, 6 ) , "WeekEnd" , "WeekDay" ) ) ) # dtaAgg2.b is also for studying what happens # finally, run the whole pipeline of calculations dtaAgg2 <- ( dta # tidyr way of making long form data %>% gather( XDates, value, -Prod_name ) %>% mutate( Dates = as.Date( XDates, format="X%d.%m.%Y" ) , WkPart = ifelse( wday( Dates ) %in% c( 0, 6 ) , "WeekEnd" , "WeekDay" ) ) %>% group_by( Prod_name, WkPart ) %>% summarise( SumOfValues = sum( value ) ) ) # the group_by and summarise steps work together On Sun, 4 Sep 2016, Filippos Katsios wrote: Dear all, I believe that this will be a more helpful way to put the problem: structure(list(Prod_name = c("Banana", "Apple", "Orange", "Yoghurt", "Eggs", "Milk", "Day_num"), X1.1.2000 = c("1", "0", "4", "3", "6", "2", "1"), X2.1.2000 = c("2", "4", "1", "5", "3", "0", "2" ), X3.1.2000 = c("1", "5", "2", "3", "0", "4", "3"), X4.1.2000 = c("2", "4", "4", "1", "0", "0", "4"), X5.1.2000 = c("0", "0", "1", "0", "2", "3", "5"), X6.1.2000 = c("1", "3", "2", "1", "4", "1", "6" ), X7.1.2000 = c("5", "4", "5", "2", "2", "1", "7")), .Names = c("Prod_name", "X1.1.2000", "X2.1.2000", "X3.1.2000", "X4.1.2000", "X5.1.2000", "X6.1.2000", "X7.1.2000"), row.names = c(NA, 7L), class = "data.frame") and the code: https://gist.github.com/anonymous/750b02ad5db448d45c92a79059bf9844 Thank you for your help Filippos On 4 September 2016 at 19:30, Jeff Newmillerwrote: Please use Reply-all to keep the mailing list in the loop. I cannot provide private assistance, and others may provide valuable input or respond faster than I can. It is very common that people cannot provide the original data. That means more work for YOU, though, not for us. It is up to you to create
Re: [R] impossible # of errors in a simple code
pretty sure you just missed the `{` at the beginning of the `function` definition block. On Sun, Sep 4, 2016 at 7:38 AM, Michael Deweywrote: > A useful rule is to fix the first error you understand and hope that the > others go away. > > On 04/09/2016 04:05, Tamar Michaeli wrote: > >> Any help in resolving the following errors will be appreciated: >> >> pollutantmean <- function(directory, pollutant, id=1:332) >>> >> + file_inc <- list.files("specdata", full.names=TRUE) >> > > So what did you hope your function was going to do, if you called it? > > dat <- data.frame() >>> for(i in 1:10) { >>> >> + dat <- rbind(dat, read.csv(file_inc[i])) >> + } >> Error in read.table(file = file, header = header, sep = sep, quote = >> quote, >> : >> object 'file_inc' not found >> > > Why did you think at this point that you had an object called file_inc? > > sulfate <- subset(dat( , 2)) >>> >> Error in subset(dat(, 2)) : could not find function "dat" >> > > So you did not manage anywhere to define a function called dat. > > nitrate <- subset(dat( , 3)) >>> >> Error in subset(dat(, 3)) : could not find function "dat" >> >>> mean(pollutant) >>> >> Error in mean(pollutant) : object 'pollutant' not found >> > > You have never defined it. > > save"pollutantmean.R" >>> >> Error: unexpected string constant in "save"pollutantmean.R"" >> > > ?save might help you here. > > > >> [[alternative HTML version deleted]] >> >> > > Please do not post in HTML as it makes your post unreadable. > >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >> > -- > Michael > http://www.dewey.myzen.co.uk/home.html > > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posti > ng-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] impossible # of errors in a simple code
> On Sep 3, 2016, at 8:05 PM, Tamar Michaeliwrote: > > Any help in resolving the following errors will be appreciated: > >> pollutantmean <- function(directory, pollutant, id=1:332) > + file_inc <- list.files("specdata", full.names=TRUE) >> dat <- data.frame() >> for(i in 1:10) { > + dat <- rbind(dat, read.csv(file_inc[i])) > + } > Error in read.table(file = file, header = header, sep = sep, quote = quote, > : > object 'file_inc' not found >> sulfate <- subset(dat( , 2)) > Error in subset(dat(, 2)) : could not find function "dat" >> nitrate <- subset(dat( , 3)) > Error in subset(dat(, 3)) : could not find function "dat" >> mean(pollutant) > Error in mean(pollutant) : object 'pollutant' not found >> save"pollutantmean.R" > Error: unexpected string constant in "save"pollutantmean.R"" This is pretty clearly an effort at solving a homework problem for Peng's R Coursera course. Homework questions are off-topic on Rhelp (and are not really encouraged on StackOverflow but you can probably find a worked example there by searching on " [r] pollutant list.files specdata".) When I tried out the course a few years ago the the participants were advised to post their questions at a website established for the course. -- David. > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Treating a vector of characters as object names to create list
Time for an R tutorial or two to learn how to use the "apply" family in R. I think what you want is: merged_list <- lapply(merging, get) -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Sep 4, 2016 at 2:57 PM, Ryan Utzwrote: > Hello, > > I have a vector of characters that I know will be object names and I'd like > to treat this vector as a series of names to create a list. But, for the > life of me, I cannot figure out how to treat a vector of characters as a > vector of object names when creating a list. > > For example, this does exactly what I want to do (with 'merged.parameters' > as the end goal): > > ### > merging=c('alkalinity','iron') > alkalinity=c('39086','29801','90410','00410') > iron=c('01045','01046') > merged.parameters=list(alkalinity,iron) > ### > > But, say I have many, many parameters in 'merging' beyond alkalinity and > iron and I'd like to just cleanly turn the elements in 'merging' into a > list. This does not work: > > ### > merged.parameters=list(get(merging)) > ### > > because it's only grabbing the first element of 'merging', for some reason. > Any advice? This feels like it really should be easy... > > -- > > Ryan Utz, Ph.D. > Assistant professor of water resources > *chatham**UNIVERSITY* > Home/Cell: (724) 272-7769 > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] element wise pattern recognition and string substitution
Dear list, I have a vector of strings that cannot be described by one pattern. So let's say I construct a vector of patterns in the same length as the vector of strings, can I do the element wise pattern recognition and string substitution. For example, pattern1 <- "([^.]*)\\.([^.]*\\.[^.]*)\\.(.*)" pattern2 <- "([^.]*)\\.([^.]*)\\.(.*)" patterns <- c(pattern1,pattern2) strings <- c('TX.WT.CUT.mean','mg.tx.cv') Say I want to extract "WT.CUT" from the first string and "tx" from the second string. If I do sub(patterns, '\\2', strings), only the first pattern will be used. looping the patterns doesn't work the way I want. Appreciate any comments. Thanks. Jun [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Treating a vector of characters as object names to create list
Hello, I have a vector of characters that I know will be object names and I'd like to treat this vector as a series of names to create a list. But, for the life of me, I cannot figure out how to treat a vector of characters as a vector of object names when creating a list. For example, this does exactly what I want to do (with 'merged.parameters' as the end goal): ### merging=c('alkalinity','iron') alkalinity=c('39086','29801','90410','00410') iron=c('01045','01046') merged.parameters=list(alkalinity,iron) ### But, say I have many, many parameters in 'merging' beyond alkalinity and iron and I'd like to just cleanly turn the elements in 'merging' into a list. This does not work: ### merged.parameters=list(get(merging)) ### because it's only grabbing the first element of 'merging', for some reason. Any advice? This feels like it really should be easy... -- Ryan Utz, Ph.D. Assistant professor of water resources *chatham**UNIVERSITY* Home/Cell: (724) 272-7769 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Treating a vector of characters as object names to create list
Hi Ryan, How about: names(merged.parameters)<-merging Jim On Mon, Sep 5, 2016 at 7:57 AM, Ryan Utzwrote: > Hello, > > I have a vector of characters that I know will be object names and I'd like > to treat this vector as a series of names to create a list. But, for the > life of me, I cannot figure out how to treat a vector of characters as a > vector of object names when creating a list. > > For example, this does exactly what I want to do (with 'merged.parameters' > as the end goal): > > ### > merging=c('alkalinity','iron') > alkalinity=c('39086','29801','90410','00410') > iron=c('01045','01046') > merged.parameters=list(alkalinity,iron) > ### > > But, say I have many, many parameters in 'merging' beyond alkalinity and > iron and I'd like to just cleanly turn the elements in 'merging' into a > list. This does not work: > > ### > merged.parameters=list(get(merging)) > ### > > because it's only grabbing the first element of 'merging', for some reason. > Any advice? This feels like it really should be easy... > > -- > > Ryan Utz, Ph.D. > Assistant professor of water resources > *chatham**UNIVERSITY* > Home/Cell: (724) 272-7769 > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Treating a vector of characters as object names to create list
On 09/05/2016 12:07 AM, Bert Gunter wrote: Time for an R tutorial or two to learn how to use the "apply" family in R. I think what you want is: merged_list <- lapply(merging, get) Or even: named_merged_list <- mget(merging) Anyway, probably you could arrive to a list of parameters directly. (E.g., if you import the parameter values from an external source or if they are the return values of a function, etc.). -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Sep 4, 2016 at 2:57 PM, Ryan Utzwrote: Hello, I have a vector of characters that I know will be object names and I'd like to treat this vector as a series of names to create a list. But, for the life of me, I cannot figure out how to treat a vector of characters as a vector of object names when creating a list. For example, this does exactly what I want to do (with 'merged.parameters' as the end goal): ### merging=c('alkalinity','iron') alkalinity=c('39086','29801','90410','00410') iron=c('01045','01046') merged.parameters=list(alkalinity,iron) ### But, say I have many, many parameters in 'merging' beyond alkalinity and iron and I'd like to just cleanly turn the elements in 'merging' into a list. This does not work: ### merged.parameters=list(get(merging)) ### because it's only grabbing the first element of 'merging', for some reason. Any advice? This feels like it really should be easy... -- Ryan Utz, Ph.D. Assistant professor of water resources *chatham**UNIVERSITY* Home/Cell: (724) 272-7769 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Treating a vector of characters as object names to create list
Thank you, Dénes. Better yet. -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Sep 4, 2016 at 4:13 PM, Dénes Tóthwrote: > > > On 09/05/2016 12:07 AM, Bert Gunter wrote: >> >> Time for an R tutorial or two to learn how to use the "apply" family >> in R. I think what you want is: >> >> merged_list <- lapply(merging, get) >> > > Or even: > named_merged_list <- mget(merging) > > Anyway, probably you could arrive to a list of parameters directly. (E.g., > if you import the parameter values from an external source or if they are > the return values of a function, etc.). > >> >> -- Bert >> >> >> >> Bert Gunter >> >> "The trouble with having an open mind is that people keep coming along >> and sticking things into it." >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >> >> >> On Sun, Sep 4, 2016 at 2:57 PM, Ryan Utz wrote: >>> >>> Hello, >>> >>> I have a vector of characters that I know will be object names and I'd >>> like >>> to treat this vector as a series of names to create a list. But, for the >>> life of me, I cannot figure out how to treat a vector of characters as a >>> vector of object names when creating a list. >>> >>> For example, this does exactly what I want to do (with >>> 'merged.parameters' >>> as the end goal): >>> >>> ### >>> merging=c('alkalinity','iron') >>> alkalinity=c('39086','29801','90410','00410') >>> iron=c('01045','01046') >>> merged.parameters=list(alkalinity,iron) >>> ### >>> >>> But, say I have many, many parameters in 'merging' beyond alkalinity and >>> iron and I'd like to just cleanly turn the elements in 'merging' into a >>> list. This does not work: >>> >>> ### >>> merged.parameters=list(get(merging)) >>> ### >>> >>> because it's only grabbing the first element of 'merging', for some >>> reason. >>> Any advice? This feels like it really should be easy... >>> >>> -- >>> >>> Ryan Utz, Ph.D. >>> Assistant professor of water resources >>> *chatham**UNIVERSITY* >>> Home/Cell: (724) 272-7769 >>> >>> [[alternative HTML version deleted]] >>> >>> __ >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] evaluating expressions as R console
Dear R users, I am trying to simulate a pseudo R console, evaluating commands. This is a simple example of a series of command lines which get evaluated: foo <- function(x) { print(x) } print)foo) foo(2) If I copied and pasted this example in an R console, this is the result to replicate (and the benchmark to compare everything else): > foo <- function(x) { + print(x) + } > print)foo) Error: unexpected ')' in "print)" > foo(2) [1] 2 The trouble I'm having is to reproduce this exact output, by evaluating the chunk of code. I tried both Rscript and littler, but I am unable to reproduce this. I had some success via: R CMD BATCH -q foo.R (saving the chunk to a file called foo.R), which only works until the first error appears. I can run it again on the subsequent command(s) after the error, but the function foo(), which in a normal R console gets created without any error, doesn't get preserved on the next run. I also had some limited success with: source("foo.R", echo = TRUE, keep.source = TRUE) But the error message is different, and the first 4 lines are not echo-ed. source() works pretty well if there are no errors, but otherwise I get the same result as R CMD BATCH, namely only the error is displayed and the function foo() doesn't get created in the current environment. I would rather use source() than R CMD BATCH, due to specific environments where the chunk should be evaluated in (not impossible, but inconvenient to save environments and load them back in the R CMD BATCH). Was also curious about knitr and pander, but again unable to replicate the results in the real R console. After many hours of searching, reading and testing, I would be grateful for any hint. Thank you in advance, Adrian -- Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr.90 050663 Bucharest sector 5 Romania [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] evaluating expressions as R console
You might want to look at the "evaluate" package. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Sep 4, 2016 at 4:47 PM, Adrian Dușawrote: > Dear R users, > > I am trying to simulate a pseudo R console, evaluating commands. This is a > simple example of a series of command lines which get evaluated: > > foo <- function(x) { > print(x) > } > print)foo) > foo(2) > > > If I copied and pasted this example in an R console, this is the result to > replicate (and the benchmark to compare everything else): > >> foo <- function(x) { > + print(x) > + } >> print)foo) > Error: unexpected ')' in "print)" >> foo(2) > [1] 2 > > > The trouble I'm having is to reproduce this exact output, by evaluating the > chunk of code. I tried both Rscript and littler, but I am unable to > reproduce this. > > I had some success via: > > R CMD BATCH -q foo.R > > (saving the chunk to a file called foo.R), which only works until the first > error appears. I can run it again on the subsequent command(s) after the > error, but the function foo(), which in a normal R console gets created > without any error, doesn't get preserved on the next run. > > I also had some limited success with: > > source("foo.R", echo = TRUE, keep.source = TRUE) > > But the error message is different, and the first 4 lines are not echo-ed. > > source() works pretty well if there are no errors, but otherwise I get the > same result as R CMD BATCH, namely only the error is displayed and the > function foo() doesn't get created in the current environment. > > I would rather use source() than R CMD BATCH, due to specific environments > where the chunk should be evaluated in (not impossible, but inconvenient to > save environments and load them back in the R CMD BATCH). > > Was also curious about knitr and pander, but again unable to replicate the > results in the real R console. > > After many hours of searching, reading and testing, I would be grateful for > any hint. > > Thank you in advance, > Adrian > > -- > Adrian Dusa > University of Bucharest > Romanian Social Data Archive > Soseaua Panduri nr.90 > 050663 Bucharest sector 5 > Romania > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] element wise pattern recognition and string substitution
Your opening assertion is false. Provide a reproducible example and someone will demonstrate. -- Sent from my phone. Please excuse my brevity. On September 4, 2016 9:06:59 PM PDT, Jun Shenwrote: >Dear list, > >I have a vector of strings that cannot be described by one pattern. So >let's say I construct a vector of patterns in the same length as the >vector >of strings, can I do the element wise pattern recognition and string >substitution. > >For example, > >pattern1 <- "([^.]*)\\.([^.]*\\.[^.]*)\\.(.*)" >pattern2 <- "([^.]*)\\.([^.]*)\\.(.*)" > >patterns <- c(pattern1,pattern2) >strings <- c('TX.WT.CUT.mean','mg.tx.cv') > >Say I want to extract "WT.CUT" from the first string and "tx" from the >second string. If I do > >sub(patterns, '\\2', strings), only the first pattern will be used. > >looping the patterns doesn't work the way I want. Appreciate any >comments. >Thanks. > >Jun > > [[alternative HTML version deleted]] > >__ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] element wise pattern recognition and string substitution
Well, he did provide an example, and... > z <- c('TX.WT.CUT.mean','mg.tx.cv') > sub("^.+?\\.(.+)\\.[^.]+$","\\1",z) [1] "WT.CUT" "tx" ## seems to do what was requested. Jeff would have to amplify on his initial statement however: do you mean that separate patterns can always be combined via "|" ? Or something deeper? Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Sep 4, 2016 at 9:30 PM, Jeff Newmillerwrote: > Your opening assertion is false. > > Provide a reproducible example and someone will demonstrate. > -- > Sent from my phone. Please excuse my brevity. > > On September 4, 2016 9:06:59 PM PDT, Jun Shen wrote: >>Dear list, >> >>I have a vector of strings that cannot be described by one pattern. So >>let's say I construct a vector of patterns in the same length as the >>vector >>of strings, can I do the element wise pattern recognition and string >>substitution. >> >>For example, >> >>pattern1 <- "([^.]*)\\.([^.]*\\.[^.]*)\\.(.*)" >>pattern2 <- "([^.]*)\\.([^.]*)\\.(.*)" >> >>patterns <- c(pattern1,pattern2) >>strings <- c('TX.WT.CUT.mean','mg.tx.cv') >> >>Say I want to extract "WT.CUT" from the first string and "tx" from the >>second string. If I do >> >>sub(patterns, '\\2', strings), only the first pattern will be used. >> >>looping the patterns doesn't work the way I want. Appreciate any >>comments. >>Thanks. >> >>Jun >> >> [[alternative HTML version deleted]] >> >>__ >>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.