[R] data frame names in sequence
I've been stuck with this problem for a whole afternoon. It's silly but totally pissed me off. I have a set of data frames with names in a sequence: df_1, df_2, df_3, ..., df_20. Now I want to access each data frame (read or write) in a for loop, in a way something like this: for (i in 1:20) { df_i - ## length(which(df_i[,7]==1)) ## } I tried paste or cat (df_, i, sep=). But neither way works. Your help is highly appreciated!! Thanks in advance! -- View this message in context: http://n4.nabble.com/data-frame-names-in-sequence-tp1010518p1010518.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] data frame names in sequence. please help!!!
On Sun, Jan 10, 2010 at 7:16 AM, Berend Hasselman b...@xs4all.nl wrote: Zoho wrote: I've been stuck with this problem for a whole afternoon. It's silly but totally pissed me off. I have a set of data frames with names in a sequence: df_1, df_2, df_3, ..., df_20. Now I want to access each data frame (read or write) in a for loop, in a way something like this: for (i in 1:20) { df_i - ## length(which(df_i[,7]==1)) ## } I tried paste or cat (df_, i, sep=). But neither way works. Your help is highly appreciated!! Thanks in advance! df_1 - data.frame(x1=3,x2=5) df_2 - data.frame(x1=2,x2=7) df_3 - data.frame(x1=-1,x2=1) for(k in 1:3){v - paste(df_,k,sep=); print(get(v))} for(k in 1:3){v - paste(df,k,sep=_); print(get(v)[,2])} Have a look at get: ?get Or better still, have a look at making a *list* instead of a bunch of data frames with numbers in their names, then you can index in a sensible way without having to construct names with paste and get. Here's a list of data frames: L = list() for(i in 1 :10){ L[[i]]=data.frame(x=runif(10)) } Now you can loop over L[[i]] This has been asked a zillion times on R-help. Sure, if you've already mistakenly created 200 data frames then you need the paste/get solution, but don't make the same mistake twice. Use a list. Barry __ 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] data frame names in sequence
?get for (i in 1:20) { df_i - get(paste('df_', i, sep='')) length(which(df_i[,7]==1)) ## } On Sat, Jan 9, 2010 at 7:57 PM, Zoho ynp...@gmail.com wrote: I've been stuck with this problem for a whole afternoon. It's silly but totally pissed me off. I have a set of data frames with names in a sequence: df_1, df_2, df_3, ..., df_20. Now I want to access each data frame (read or write) in a for loop, in a way something like this: for (i in 1:20) { df_i - ## length(which(df_i[,7]==1)) ## } I tried paste or cat (df_, i, sep=). But neither way works. Your help is highly appreciated!! Thanks in advance! -- View this message in context: http://n4.nabble.com/data-frame-names-in-sequence-tp1010518p1010518.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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]] __ 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] data frame names in sequence. please help!!!
Thank you all. The 'list' works well, except makes a really big 'list', since my data is 'huge'. But solves the problem anyway. Appreciate a lot! Barry Rowlingson wrote: On Sun, Jan 10, 2010 at 7:16 AM, Berend Hasselman b...@xs4all.nl wrote: Zoho wrote: I've been stuck with this problem for a whole afternoon. It's silly but totally pissed me off. I have a set of data frames with names in a sequence: df_1, df_2, df_3, ..., df_20. Now I want to access each data frame (read or write) in a for loop, in a way something like this: for (i in 1:20) { df_i - ## length(which(df_i[,7]==1)) ## } I tried paste or cat (df_, i, sep=). But neither way works. Your help is highly appreciated!! Thanks in advance! df_1 - data.frame(x1=3,x2=5) df_2 - data.frame(x1=2,x2=7) df_3 - data.frame(x1=-1,x2=1) for(k in 1:3){v - paste(df_,k,sep=); print(get(v))} for(k in 1:3){v - paste(df,k,sep=_); print(get(v)[,2])} Have a look at get: ?get Or better still, have a look at making a *list* instead of a bunch of data frames with numbers in their names, then you can index in a sensible way without having to construct names with paste and get. Here's a list of data frames: L = list() for(i in 1 :10){ L[[i]]=data.frame(x=runif(10)) } Now you can loop over L[[i]] This has been asked a zillion times on R-help. Sure, if you've already mistakenly created 200 data frames then you need the paste/get solution, but don't make the same mistake twice. Use a list. Barry __ 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. -- View this message in context: http://n4.nabble.com/data-frame-names-in-sequence-please-help-tp1010518p1010715.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] data frame names in sequence. please help!!!
Zoho wrote: I've been stuck with this problem for a whole afternoon. It's silly but totally pissed me off. I have a set of data frames with names in a sequence: df_1, df_2, df_3, ..., df_20. Now I want to access each data frame (read or write) in a for loop, in a way something like this: for (i in 1:20) { df_i - ## length(which(df_i[,7]==1)) ## } I tried paste or cat (df_, i, sep=). But neither way works. Your help is highly appreciated!! Thanks in advance! df_1 - data.frame(x1=3,x2=5) df_2 - data.frame(x1=2,x2=7) df_3 - data.frame(x1=-1,x2=1) for(k in 1:3){v - paste(df_,k,sep=); print(get(v))} for(k in 1:3){v - paste(df,k,sep=_); print(get(v)[,2])} Have a look at get: ?get Berend -- View this message in context: http://n4.nabble.com/data-frame-names-in-sequence-please-help-tp1010518p1010585.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.