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

wk y vivacity at hotmail.com
Tue Oct 13 02:29:14 CEST 2009


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".


>cmbine$names
apple tiger pencil chicken banana pear

>cmbine$mass
0.50 100.00 0.01 1.00 0.15 0.30

>cmbine$classes
1 2 3 2 1 1

These are the results which I want to obtain:

>df1
classes  mass
apple  0.50
tiger  100.00
pencil  0.01

>df2
classes  mass
banana  0.15
chicken  1.00

>df3
classes  mass
pear  0.30

Below shows what I have tried. The main problem I have = I don't know how to assign the selected instance into a new dataframe with a name which is generated 'on-the-fly' based on the value of j (the jth row).


for (i in 1:3) {
same_cell <- cmbine[cmbine$classes == i, ]
if (nrow(same_cell)!=0){
  for (j in 1:nrow(same_cell)){
    picked <- same_cell[j, ]
    assign(paste("df", j, sep=""), picked)
    #assign(paste("df",j, sep=""), paste("df", j, sep=""))
  }
}

 
The problem is that the assign function overwrites the previous value of df and therefore the I have not been able to insert rows in the three df dataframes and always end up with only 1 (final) row in df1, df2 and df3. I have tried using rbind but was not able to assign values back to the "on-the-fly" variable names.
 
I really need your advice and assistance since I have stuck with this for some time now.
 
Thank you.




More information about the R-help mailing list