[R] data frame names in sequence. please help!!!

Berend Hasselman bhh at xs4all.nl
Sun Jan 10 08:16:04 CET 2010




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.



More information about the R-help mailing list