Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through and modify multiple data frames in R

Tags:

loops

dataframe

r

I have data frames A, B, C, ... and want to modify each data frame in the same way, e.g. re-ordering factors levels of a factor which is present in all of the data frames:

A = data.frame( x=c('x','x','y','y','z','z') )
B = data.frame( x=c('x','y','z') )
C = data.frame( x=c('x','x','x','y','y','y','z','z','z') )

A$x = factor( A$x, levels=c('z','y','x') )
B$x = factor( B$x, levels=c('z','y','x') )
C$x = factor( C$x, levels=c('z','y','x') )

This gets laborious if there are lots of data frames and/or lots of modifications to be done. How can I do it concisely, using a loop or something better? A straightforward approach like

for ( D in list( A, B, C ) ) {
D$x = factor( D$x, levels=c('z','y','x') )
}

does not work, because it doesn't modify the original data frames.

EDIT: added definitions of A, B, and C to make it reproducible.

like image 786
baixiwei Avatar asked Nov 02 '13 03:11

baixiwei


People also ask

How do I apply a function to multiple data frames in R?

To join more than two (multiple) R data frames use the reduce() function from tidyverse package. This function takes all the data frames as a list and joins the data frames based on the specified column.

Can you Rbind multiple data frames in R?

The rbind() function in R and the bind_rows() function are the most useful functions when it comes to data manipulation. You can easily bind two data frames of the same column count using rbind() function.

How do I add a data frame to a loop in R?

Within the for-loop we have performed three steps: First, we have created a vector object containing the values that we wanted to add as column to our data frame. Second, we added the new column ad the end of our data frame. Third, we renamed our new column (this step is optional).


1 Answers

One thing to note about R is that, with respect to assignment, <- is transitive, whereas = is not. Thus, if your data frames are all the same in this respect, you should be able to do something like this:

A$x <- B$x <- C$x <- factor( C$x, levels=c('z','y','x') )
like image 126
gung - Reinstate Monica Avatar answered Nov 12 '22 21:11

gung - Reinstate Monica