[R] How to apply five lines of code to ten dataframes?

guohao.huang at gmail.com guohao.huang at gmail.com
Mon Dec 7 16:17:52 CET 2009


For example,
tt1 = 2:4
ind = 1
evalstr = paste("xx = tt", ind, sep = "")
eval(parse(text=evalstr))
==> x = 2, 3, 4

It's easy for you to write a for loop to solve your problems

                                                                             
                                Guo-Hao Huang

--------------------------------------------------
From: "Mark Na" <mtb954 at gmail.com>
Sent: Monday, December 07, 2009 11:07 PM
To: <r-help at r-project.org>
Subject: [R] How to apply five lines of code to ten dataframes?

> Hello R-helpers,
>
> I have 10 dataframes (named data1, data2, ... data10) and I would like to
> add 5 new columns to each dataframe using the following code:
>
> data1$LogDepth<-log10(data1[,2]/data1[,4])
> data1$LogArea<-log10(data1[,3]/data1[,5])
> data1$p<-2*data1[,6]/data1[,7]
> data1$Exp<-data1[,2]^(2/data1[,8])
> data1$s<-data1[,3]/data1[,9]
>
> ...but I would prefer not to repeat this chunk of code 10 times!
>
> I have struggled with setting up a loop to apply these 5 lines of code to
> each of the 10 dataframes, but I'm not having much luck.
>
> Any help would be much appreciated.
>
> Thank you, Mark
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list