separent
2009-Nov-17 23:15 UTC
[R] Perform operations on dataframes called with paste in loops
In a loop, I compose the name of a csv file using paste, then read it (e.g., dataset1.csv, dataset2.csv, etc). The name of the dataframe assigned to the imported csv is also composed with paste (e.g., dataset1, dataset2, etc.). Now I want to perform operations on the dataframes dataset1, dataset2, etc. However, the paste function only renders a string on which I can not, for example, do operations like plot(paste("dataset",i,"[,1]",sep=""),paste("dataset",i,"[,2]",sep="")). How could I call the dataframe instead of the string representing its name? -- Serge-?tienne Parent Golder Associ?s Montr?al -- View this message in context: http://old.nabble.com/Perform-operations-on-dataframes-called-with-paste-in-loops-tp26399586p26399586.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2009-Nov-17 23:54 UTC
[R] Perform operations on dataframes called with paste in loops
On Nov 17, 2009, at 6:14 PM, separent wrote:> > In a loop, I compose the name of a csv file using paste, then read > it (e.g., > dataset1.csv, dataset2.csv, etc). The name of the dataframe assigned > to the > imported csv is also composed with paste (e.g., dataset1, dataset2, > etc.). > Now I want to perform operations on the dataframes dataset1, > dataset2, etc. > However, the paste function only renders a string on which I can > not, for > example, do operations like > plot(paste("dataset",i,"[,1]",sep=""),paste("dataset",i,"[, > 2]",sep="")). How > could I call the dataframe instead of the string representing its > name??get (It's also in the FAQ.) -- David
Uwe Ligges
2009-Nov-18 08:05 UTC
[R] Perform operations on dataframes called with paste in loops
separent wrote:> In a loop, I compose the name of a csv file using paste, then read it (e.g., > dataset1.csv, dataset2.csv, etc). The name of the dataframe assigned to the > imported csv is also composed with paste (e.g., dataset1, dataset2, etc.). > Now I want to perform operations on the dataframes dataset1, dataset2, etc. > However, the paste function only renders a string on which I can not, for > example, do operations like > plot(paste("dataset",i,"[,1]",sep=""),paste("dataset",i,"[,2]",sep="")). How > could I call the dataframe instead of the string representing its name?1. consider to read the csv files into a *list* of dataframes and loop over that list. 2. if you do not want to follow 1., see ?get Uwe Ligges
Greg Snow
2009-Nov-18 21:03 UTC
[R] Perform operations on dataframes called with paste in loops
There are a few options: You can read the help page for the function that you used to assign names to the data frames when you read them in (the 'see also' section is there for a reason). You can read the FAQ (7.21 to be specific, but the others could save you re-asking FAQs in the future) You can take a better approach by reading all your data sets into a list (use lapply on the vector of names), then use lapply on the list of datasets and avoid all the future headache/heartache that will come from the approach you are trying. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of separent > Sent: Tuesday, November 17, 2009 4:15 PM > To: r-help at r-project.org > Subject: [R] Perform operations on dataframes called with paste in > loops > > > In a loop, I compose the name of a csv file using paste, then read it > (e.g., > dataset1.csv, dataset2.csv, etc). The name of the dataframe assigned to > the > imported csv is also composed with paste (e.g., dataset1, dataset2, > etc.). > Now I want to perform operations on the dataframes dataset1, dataset2, > etc. > However, the paste function only renders a string on which I can not, > for > example, do operations like > plot(paste("dataset",i,"[,1]",sep=""),paste("dataset",i,"[,2]",sep="")) > . How > could I call the dataframe instead of the string representing its name? > -- > View this message in context: http://old.nabble.com/Perform-operations- > on-dataframes-called-with-paste-in-loops-tp26399586p26399586.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.