[R] loop through and modify multiple data frames

arun smartpink111 at yahoo.com
Fri Jun 29 15:11:53 CEST 2012


Hi Jan,

Glad it helped you in some way.  I am also copying to rhelp as your solution might be useful for future reference.

A.K.



----- Original Message -----
From: Jan Näs <skydiver.jan at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: 
Sent: Friday, June 29, 2012 5:02 AM
Subject: Re: [R] loop through and modify multiple data frames

Thank you, this helped a bit on the way. I managed to loop through the
data frames by haveing an array with data frame names (ex animals <-
("cats","dogs"...etc) ) same as names in the list, to get the results
and also split it up back into separate data frames which I was after.
something like this ex:

for(i in 1:length(animals)){
assign(animals[i] , cbind(as.data.frame(myList[i]), cost))
}

/Jan

2012/6/29 arun <smartpink111 at yahoo.com>:
> Hello,
> I tried loop to modify the list, but unfortunately it was not successful.  But, I found another way to get the result you wanted (at least from what I understand).
>
> #Here are the datasets
>
> cats<-read.table(text="
> name eats_kg
> 1 bob 3
> 2 garfield 4
> 3 chuck 6
> ", sep="",header=TRUE)
> dogs<-read.table(text="
> name eats_kg
> 1 gob 7
> 2 rofield 8
> 3 norris 9
> 4 rody 6
> ", sep="",header=TRUE)
>
> birds<-read.table(text="
> name eats_kg
> 1 jud 0.5
> 2 Trud 0.4
> 3 Sind 0.6
> 4 Rav 0.8
> ", sep="",header=TRUE)
>
>
> mylist<-list(cats=cats,dogs=dogs,birds=birds)
> mydf<-do.call("rbind",mylist)
>  price<-c(rep(20,3),rep(25,4),rep(15,4))
>  mydf$cost_food<-mydf$eats_kg*price
>
> animal<-c(rep("cats",3),rep("dogs",4),rep("birds",4))
> mydfnew<-data.frame(animal,mydf)
> row.names(mydfnew)<-1:nrow(mydfnew)
> mylistnew<-split(mydfnew,list(mydfnew$animal),drop=TRUE)
> mylistnew1<-list(cats=mylistnew[[2]][-1],dogs=mylistnew[[3]][-1],birds=mylistnew[[1]][-1])
> row.names(mylistnew1$dogs)<-1:nrow(mylistnew1$dogs)
> row.names(mylistnew1$birds)<-1:nrow(mylistnew1$birds)
>> mylistnew1
> $cats
>       name eats_kg cost_food
> 1      bob       3        60
> 2 garfield       4        80
> 3    chuck       6       120
>
> $dogs
>      name eats_kg cost_food
> 1     gob       7       175
> 2 rofield       8       200
> 3  norris       9       225
> 4    rody       6       150
>
> $birds
>   name eats_kg cost_food
> 1  jud     0.5       7.5
> 2 Trud     0.4       6.0
> 3 Sind     0.6       9.0
> 4  Rav     0.8      12.0
>
> There might be an easy way in loop to get the same result.
>
> A.K.
>
>
>
>
>
>
>
> ----- Original Message -----
> From: Jan Näs <jan at plattfot.com>
> To: r-help at r-project.org
> Cc:
> Sent: Thursday, June 28, 2012 5:10 AM
> Subject: [R] loop through and modify multiple data frames
>
> 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.
>
> ______________________________________________
> 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