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
frames:

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


Then, I usually resort to a gratuitous application of lapply() and
do.call():

# 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
results
        # together.
        ithRows <- do.call(rbind,lapply(byClass,function(df){
          
          return( df[i,] )
          
        }))
        
        # Remove all NA rows
        ithRows <- ithRows[ !is.na(ithRows[,1]), ]
        
        return(ithRows)
        
})


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

[[2]]
    names mass classes
1  banana 0.15       1
2 chicken 1.00       2

[[3]]
  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

-----
Charlie Sharpsteen
Undergraduate
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.

______________________________________________
R-help@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.

Reply via email to