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

hadley wickham h.wickham at gmail.com
Tue Oct 13 15:44:35 CEST 2009


On Tue, Oct 13, 2009 at 6:57 AM, Ista Zahn <istazahn at gmail.com> wrote:
> I'm sure there's a really cool way to do this with plyr, although I
> don't know if my particular plyr version is much better. Anyway here
> it is:
>
> cmbine <- read.csv(textConnection('names, mass, classes
> apple,0.50,1
> tiger,100.00,2
> pencil,0.01,3
> chicken,1.00,2
> banana,0.15,1
> pear,0.30,1'))
>
> library(plyr)
>
> dfl <- list()
>
> for(i in 1:max(cmbine$classes)) {
>  dfl[[i]] <- ddply(cmbine, .(classes), function(x) {x[i,]})
> }

Here's another approach:

cmbine <- read.csv(textConnection('names, mass, classes
apple,0.50,1
tiger,100.00,2
pencil,0.01,3
chicken,1.00,2
banana,0.15,1
pear,0.30,1'))

cmbine <- ddply(cmbine, "classes", transform, i = seq_along(names))
dlply(cmbine, "i")

Hadley

-- 
http://had.co.nz/




More information about the R-help mailing list