[R] Particular Cross tables
Anne York
york at noaa.gov
Mon May 20 18:44:07 CEST 2002
test.sum <- tapply(test$v1,list(test$f1,test$f2),sum)
test.sum
1 2 3
1 90 40 40
2 50 70 NA
3 80 90 NA
to change the NA's to 0, the following works:
> test.sum[is.na(test.sum)] <- 0
> test.sum
1 2 3
1 90 40 40
2 50 70 0
3 80 90 0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Anne E. York
National Marine Mammal Laboratory
Seattle WA 98115-0070 USA
e-mail: anne.york at noaa.gov
Voice: +1 206-526-4039
Fax: +1 206-526-6615
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Sat, 18 May 2002, Kenneth Cabrera wrote:
|Dear R experts:
|
|I have this data base (data frame) with three variables
|where f1 and f2 are factors.
|I want to obtain a table with the sum of the third variable
|at each cross element of the two factors.
|
|Would you help me with any function idea?
|
|Original data base (data frame)
|
|f1 f2 v1
|1 2 10
|1 1 20
|1 2 30
|1 3 40
|2 1 50
|2 2 60
|1 1 70
|3 1 80
|3 2 90
|2 2 10
|
|I would like to obtain this matrix (sum of the crossed elements with
|zeros where I don't have any crossed factor)
|
| Factor 1
| 90 40 40
|Factor 2 50 70 0
| 80 90 0
|
|Is it possible to obtain any kind of function (not only sum())?
|(like sd(), for example?)
|
|Thank you very much for your help.
|
|Kenneth
|
|-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
|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
|_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
|
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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