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
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.
Hi r-help-bounces at r-project.org napsal dne 20.12.2010 07:07:35:> 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, nomissing values)> > How can I get a table such that v1 levels will show as rows, v2 levelsas> 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 oftable> (v1,v2) I would like to get the sum(v3), how can it be done?xtabs(v3~v1+v2, data=your.data.frame) is other option. Regards Petr> > 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.