[R] loop through and modify multiple data frames

Jan Näs jan at plattfot.com
Thu Jun 28 11:10:14 CEST 2012


Hi

Newbie question:

I have a set of data frames that I want to do the same calculations on each.
I've found out that I can put them in a list and loop through the list
to do the calculation, but not put the results back into each
data.frame..

For example three data frames cats, dogs, birds

where >cats

name eats_kg
1 bob 3
2 garfield 4
3 chuck 6

and dogs and birds are similar but not same length.

On each data frame I now want to add a column with cost of food by
calculating "eats_kg"*price of food.

So question is, can I put all data frames into one loop which returns
a new column for each?

Ive tried

MyList <- list(cast,dog,birds)

for(i in 1:length(MyList)){
price <- as.data.frame(myList[i])[,2] * cost
.. and then Im stuck. How can I put "price" back into each data frame?

Thanks in advance.



More information about the R-help mailing list