[R] manipulating the result of by()

Stefano stecalza at tiscali.it
Fri Jul 26 11:41:26 CEST 2002


Hi.

I have the following problem.
I use by() to compute a table of observed and expected counts of a variable
(eg sobjects who belong to a smoking category) within combination of 4
factors (age class, education, sex, region) for a total 4*3*2*5 tables. 
I get a list of length 120 and dim 4. 
Noww I want to sum the tables within the level of one of the grouping
variables, eg age class. How can I do?

Here is an example:

ttt <- by(data,list=c(age,sex,region,edu),FUN=function(x)
	{
	 tto <- table(x$smoke[x$grp=="study1"])
	 so <- sum(tto)
	 tte <- prop.table(table(x$smoke[x$grp=="study2"]))*so
	 ttt <- rbind(tto,tte)
	 ttt
	}

Think of age as a factor with 4 levels (age category). Now a want to sum
each table (observed=tto and expected=tte separately) within each age category.
I should end with 4 table with 2 rows (obs & exp) and 3 columns (the smoke
levels). Any hint?

TIA,

Stefano
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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