0

Simplified example is:

data<-data.frame(rnorm(10),rbinom(10,1,prob=.7))
sdata<-split(data[,1],data[,2])
ret <- lapply(sdata,mean)

I make data column and factor column. I come from SQL language and solve an,y task with grouping pattern.

lapply(sdata,mean) is:

class(ret)
[1] "list"

str(ret)
List of 2
 $ 0: num -0.146
 $ 1: num -0.0572

How can I make data frame again from list?

Are there limitation to factor type? Factor become "name" of list elements and I afraid to lose data/precision when convert from name bask to actual data in data frame.

Is there better way to process partitioned/grouped data then split/lapply?

PS Fill free to correct question wording. I have little experience with R to write professionally.

@MLavoie ret <- data.frame(lapply(sdata,mean)) gives me:

> dim(ret)
[1] 1 2

I expect 2x1.

@David Arenburg function application in lapply on result from sapply receive not only single column - but all and not only single row - but all within group. This approach may lead to performance degradation but allow any processing logic.

aggregate and data.table work on individual column in each group if I understand properly.

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • ret <- data.frame(lapply(sdata,mean)) ?? – MLavoie Feb 28 '16 at 23:07
  • 3
    [Yes, there are plenty of ways to calculate mean by group](http://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame) – David Arenburg Feb 28 '16 at 23:17
  • 1
    `split` / `lapply` may be sufficient for simple tasks like this, but IMO you are better off getting acquainted with the `data.table` [package](https://github.com/Rdatatable/data.table/wiki) sooner rather than later as it is particularly useful for aggregate operations. And generally speaking you should prefer the `character` class to the `factor` class, with certain exceptions (that don't apply here), e.g. working with statistical models. – nrussell Feb 28 '16 at 23:30

0 Answers0