[R] splitting dataframe, assign to new dataframe, add new rows to new dataframe

cls59 chuck at sharpsteen.net
Tue Oct 13 04:02:01 CEST 2009

wk yeo wrote:
> Hi, all,
> My objective is to split a dataframe named "cmbine" according to the value
> of "classes". After the split, I will take the first instance from each
> class and bin them into a new dataframe, "df1". In the 2nd iteration, I
> will take the 2nd available instance and bin them into another new
> dataframe, "df2".

My apologies, I did not read the first lines of your question carefully. Say
we split the data frame by class using by():

byClass <- by( cmbine, cmbine[['classes']], function( df ){ return(df) } )

We could then determine the maximum number of rows in all the returned data

maxRows <- max(sapply( byClass, nrow ))

Then, I usually resort to a gratuitous application of lapply() and

# Loop over each value between 1 and the maximum number of rows, return
results as a list.
lapply( 1:maxRow, function(i){

        # Loop over each data frame, extract the ith rows and rbind the
        # together.
	ithRows <- do.call(rbind,lapply(byClass,function(df){
	  return( df[i,] )
        # Remove all NA rows
	ithRows <- ithRows[ !is.na(ithRows[,1]), ]

   names  mass classes
1  apple 5e-01       1
2  tiger 1e+02       2
3 pencil 1e-02       3

    names mass classes
1  banana 0.15       1
2 chicken 1.00       2

  names mass classes
1  pear  0.3       1

There's definitely a more elegant way to do this, perhaps using some
routines in the plyr package.

Good luck!


Charlie Sharpsteen
Environmental Resources Engineering
Humboldt State University
View this message in context: http://www.nabble.com/splitting-dataframe%2C-assign-to-new-dataframe%2C-add-new-rows-to-new-dataframe-tp25865409p25866082.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list