[R] dataframes and factors

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Jun 15 13:57:16 CEST 2000


Wolfgang Koller <koller2 at fgr.wu-wien.ac.at> writes:

> Dear R-List,
> 
> I have a dataframe X containing factor f and numeric variable x1, x2, ... I
> want to create a new dataframe (or possibly a matrix) that gives statistics
> (e.g. sum) for the variables x1, x2, ... in each group defined by factor f.
> What is the simplest way to do this?
> 
> I tried:
>   attach(X)
>   Z <- data.frame(f=levels(f),x1=as.vector(lapply(split(x1,f),sum)))
> 
> and stumbled on:
>   Error in data.frame(f = levels(f), x1 = as.vector(lapply(split(x1, f),  : 
>         arguments imply differing number of rows: 5, 1
> 
> Help is much appreciated,

does aggregate(X, f, sum) do what you want?

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list