Discussion:
[R] how to loop through dataframe objects in environment
Stephen HK Wong
2014-10-15 20:28:24 UTC
Permalink
Dear All,

I have many 50 objects they are all dataframes. Each dataframe has many rows and four column. I simply want to do an addition of 3rd and 4th column and save the result into 5th column. Since there are many dataframes, I don't want to do it one by one, is there a way to loop through all objects and perform the similar action ?

One way I can think of is like this:

for (i in 1:50){
get(ls()[i])[,3]+get(ls()[i][,4]
}

But I don't know how to save the addition result back to 5th column of each dataframe.


Many Thanks!



Stephen HK Wong
Vikash Kumar
2014-10-16 03:59:01 UTC
Permalink
Hi Stephen,

Try out lapply(). It would help you loop through all data frames and sum.

Regards,
Vikash

On Thu, Oct 16, 2014 at 1:58 AM, Stephen HK Wong <honkit at stanford.edu>
Post by Stephen HK Wong
Dear All,
I have many 50 objects they are all dataframes. Each dataframe has many
rows and four column. I simply want to do an addition of 3rd and 4th column
and save the result into 5th column. Since there are many dataframes, I
don't want to do it one by one, is there a way to loop through all objects
and perform the similar action ?
for (i in 1:50){
get(ls()[i])[,3]+get(ls()[i][,4]
}
But I don't know how to save the addition result back to 5th column of each dataframe.
Many Thanks!
Stephen HK Wong
______________________________________________
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.
[[alternative HTML version deleted]]
Jeff Newmiller
2014-10-16 10:47:23 UTC
Permalink
This advice works best when you use the lapply function to load your data frames to begin with. That way your like-structured data frames are grouped into one list that you can loop through without complicated use of get and assign.

dtadir <- "mydatadir"
fnames <- list.files( dtadir )
dtalist <- lapply( fnames, function( fn ) { read.csv( file.path( dtadir, fn ) ) }
for ( idx in seq_along( dtalist ) ) {
dtalist[[ idx]]$result <- dtalist[[ idx ]][ , 3 ] + dtalist[[ idx ]][ , 4 ]
}

---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Post by Vikash Kumar
Hi Stephen,
Try out lapply(). It would help you loop through all data frames and sum.
Regards,
Vikash
On Thu, Oct 16, 2014 at 1:58 AM, Stephen HK Wong <honkit at stanford.edu>
Post by Stephen HK Wong
Dear All,
I have many 50 objects they are all dataframes. Each dataframe has
many
Post by Stephen HK Wong
rows and four column. I simply want to do an addition of 3rd and 4th
column
Post by Stephen HK Wong
and save the result into 5th column. Since there are many dataframes,
I
Post by Stephen HK Wong
don't want to do it one by one, is there a way to loop through all
objects
Post by Stephen HK Wong
and perform the similar action ?
for (i in 1:50){
get(ls()[i])[,3]+get(ls()[i][,4]
}
But I don't know how to save the addition result back to 5th column
of
Post by Stephen HK Wong
each dataframe.
Many Thanks!
Stephen HK Wong
______________________________________________
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.
[[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.
Loading...