Hello, I need a command. I have a lot of data in different dataframes(auto.0a, auto.0b, auto.0c, auto.5Na,...), that has similar names. I could print the names all at once wih a loop with the command paste(), see below: plot<-c("0a","0b","0c","5Na","5Nb","5Nc","PKa","PKb","PKc","5NPKa","5NPKb", "5NPKc","10NPKa","10NPKb","10NPKc","20NPKa","20NPKb","20NPKc") for (x in 1:length(plot)) { name<-paste("auto.",plot[x],sep="") print(name) } I want to do very similar things with all the dataframes and their structure is also the same. Is there a way to write a loop? (so that I don't have to write the same 18 times) I tried things like that: for (x in 1:length(plot)) { plot(paste("auto.",plot[x],sep="")[,1],paste("auto.",plot[x],sep="")[,2],col=...) } But it doesn't work because it just takes the character "auto.0a". Thanks a lot for your help, Sybille Wendel --
You can put all dataframes into a list L, e.g. L <- list(auto.0a, auto.0b,...) and then do either a for-loop or use lapply. For example for (ii in 1:length(L)){ plot(y~x, data=L[[ii]],...) } or lapply(L, function(d) plot(y~x, data=d) Med venlig hilsen S?ren H?jsgaard -----Oprindelig meddelelse----- Fra: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] P? vegne af Sybille Wendel Sendt: 18. juni 2008 10:36 Til: r-help at r-project.org Emne: [R] paste data Hello, I need a command. I have a lot of data in different dataframes(auto.0a, auto.0b, auto.0c, auto.5Na,...), that has similar names. I could print the names all at once wih a loop with the command paste(), see below: plot<-c("0a","0b","0c","5Na","5Nb","5Nc","PKa","PKb","PKc","5NPKa","5NPKb", "5NPKc","10NPKa","10NPKb","10NPKc","20NPKa","20NPKb","20NPKc") for (x in 1:length(plot)) { name<-paste("auto.",plot[x],sep="") print(name) } I want to do very similar things with all the dataframes and their structure is also the same. Is there a way to write a loop? (so that I don't have to write the same 18 times) I tried things like that: for (x in 1:length(plot)) { plot(paste("auto.",plot[x],sep="")[,1],paste("auto.",plot[x],sep="")[,2],col=...) } But it doesn't work because it just takes the character "auto.0a". Thanks a lot for your help, Sybille Wendel -- ______________________________________________ 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.
>I want to do very similar things with all the dataframes and their structure >is also the same. >Is there a way to write a loop? (so that I don't have to write the same 18 >times) >I tried things like that: >for (x in 1:length(plot)) >{ > plot(paste("auto.",plot[x],sep="")[,1], > paste("auto.",plot[x],sep="")[,2],col=...) >}you need to get() the data frames. for (x in 1:length(plot)) { df <- get(plot[x]) plot(df[,1],df[,2],col=...) } HTH, ido
get() is your friend. Try: for (x in 1:length(plot)) { thisdf <- paste("auto.", plot[x], sep="") plot(thisdf[, 1], thisdf[, 2], col=...) } HTH, Ray Brownrigg Sybille Wendel wrote:> Hello, > > I need a command. > I have a lot of data in different dataframes(auto.0a, auto.0b, auto.0c, auto.5Na,...), that has similar names. > > I could print the names all at once wih a loop with the command paste(), see below: > > plot<-c("0a","0b","0c","5Na","5Nb","5Nc","PKa","PKb","PKc","5NPKa","5NPKb", > "5NPKc","10NPKa","10NPKb","10NPKc","20NPKa","20NPKb","20NPKc") > > for (x in 1:length(plot)) > { > name<-paste("auto.",plot[x],sep="") > print(name) > } > > I want to do very similar things with all the dataframes and their structure is also the same. > Is there a way to write a loop? (so that I don't have to write the same 18 times) > I tried things like that: > > for (x in 1:length(plot)) > { > plot(paste("auto.",plot[x],sep="")[,1],paste("auto.",plot[x],sep="")[,2],col=...) > } > > But it doesn't work because it just takes the character "auto.0a". > > > Thanks a lot for your help, > Sybille Wendel > > > -- > > ______________________________________________ > 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.
On 18 Jun 2008, at 10:36, Sybille Wendel wrote:> I need a command. > I have a lot of data in different dataframes(auto.0a, auto.0b, auto. > 0c, auto.5Na,...), that has similar names. > > I could print the names all at once wih a loop with the command > paste(), see below: > > plot<- > c("0a","0b","0c","5Na","5Nb","5Nc","PKa","PKb","PKc","5NPKa","5NPKb", > "5NPKc","10NPKa","10NPKb","10NPKc","20NPKa","20NPKb","20NPKc") > > for (x in 1:length(plot)) > { > name<-paste("auto.",plot[x],sep="") > print(name) > } >First of all, maybe it is better to avoid to name a variable 'plot'. It works, but it could be a bit confusing. You can do this easier (paste can handle vectors etc.): name<-paste("auto.", plot, sep="")> I want to do very similar things with all the dataframes and their > structure is also the same. > Is there a way to write a loop? (so that I don't have to write the > same 18 times) > I tried things like that: > > for (x in 1:length(plot)) > { > plot(paste("auto.",plot[x],sep="")[,1],paste("auto.",plot[x],sep="") > [,2],col=...) > }paste("auto.",plot[x],sep="")[,1] => doesn't work. Assuming that 'auto.0a' is a data.frame you should use get(paste("auto",".0a",sep=''))[,1] instead to get the first column of the data.frame 'auto.0a' Maybe try: plot(get(paste("auto.",plot[x],sep=""))[,1], get(paste("auto.",plot[x],sep=""))[,2], col=...) --Hans
Sybille Wendel wrote:> Hello, > > I need a command. > I have a lot of data in different dataframes(auto.0a, auto.0b, auto.0c, auto.5Na,...), that has similar names. > > I could print the names all at once wih a loop with the command paste(), see below: > > plot<-c("0a","0b","0c","5Na","5Nb","5Nc","PKa","PKb","PKc","5NPKa","5NPKb", > "5NPKc","10NPKa","10NPKb","10NPKc","20NPKa","20NPKb","20NPKc") > > for (x in 1:length(plot)) > { > name<-paste("auto.",plot[x],sep="") > print(name) > } > > I want to do very similar things with all the dataframes and their structure is also the same. > Is there a way to write a loop? (so that I don't have to write the same 18 times) > I tried things like that: > > for (x in 1:length(plot)) > { > plot(paste("auto.",plot[x],sep="")[,1],paste("auto.",plot[x],sep="")[,2],col=...) > } > > But it doesn't work because it just takes the character "auto.0a". > >The short answer is get(), but this is an opportunity to get yoursellf acquainted with the mindframe of vectorization in R: plotNames<-c("0a","0b","0c","5Na","5Nb","5Nc","PKa","PKb","PKc","5NPKa","5NPKb", "5NPKc","10NPKa","10NPKb","10NPKc","20NPKa","20NPKb","20NPKc") frames <- lapply(paste("auto", plotNames, sep="."), get) plotOne <- function(f,n) plot(f[1:2], title="n") mapply(plotOne, frames, plotNames) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907