[R] tabulating 2 factors weighting by a third var

Gerrit Eichner Gerrit.Eichner at math.uni-giessen.de
Mon Dec 20 07:43:55 CET 2010


Hi, Luca,

if V is you data frame, maybe

with( V, tapply( v3, list( v1, v2), sum))

does what you want.

Hth  --  Gerrit


On Mon, 20 Dec 2010, Luca Meyer wrote:

> Hi,
>
> This must be an easy one but so far I haven't find a way out...
>
> I have a data frame such as:
>
> $ v1    : Factor w/ 5 levels
> $ v2    : Factor w/ 2 levels
> $ v3    : Class 'difftime'  atomic [1:6666]
>
> basically v1 and v2 are factors, while v3 is a variable containing the 
> duration of certain activities (values ranging from 11 to 45000 sec, no 
> missing values)
>
> How can I get a table such that v1 levels will show as rows, v2 levels 
> as columns and v3 is the weight by which table(v1,v2) is weighted? That 
> is, instead of getting the count of occurences in each of the 10 cells 
> of table(v1,v2) I would like to get the sum(v3), how can it be done?
>
> Thanks,
> Luca
>
> Luca Meyer
> www.lucameyer.com
> IBM SPSS Statistics release 19.0.0
> R version 2.12.1 (2010-12-16)
> Mac OS X 10.6.5 (10H574) - kernel Darwin 10.5.0
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list