hi netters Suppose I have a factor X, with 10 elements and 3 levels: A B B C A C B A C C . It is easy to count the number of elements for each level: tapply(X,X,length). Now I have another factor Y, which formed a matrix with X: X| A B B C A C B A C C Y| B B C C C A A A B B I wanna count the number of elements for each of these conditions: when X=A and Y=A; when X=A and Y=B; when X=A and Y=C; when X=B and Y=A; when X=B and Y=B; when X=B and Y=C; when X=C and Y=A; when X=C and Y=B; when X=C and Y=C. The code I have written for this task is too complicated, involving a lot of for loops and if conditions. I believe there's some nice code that can do it far more efficiently. Can anyone give me a hint? Thanks a lot!
table() is your friend:> X <- factor(scan(what=""))1: A B B C A C B A C C 11: Read 10 items> Y <- factor(scan(what=""))1: B B C C C A A A B B 11: Read 10 items> table(X)X A B C 3 3 4> table(X, Y)Y X A B C A 1 1 1 B 1 1 1 C 1 2 1 Andy> From: zhihua li > > hi netters > > Suppose I have a factor X, with 10 elements and 3 levels: A B > B C A C B A C > C . > > It is easy to count the number of elements for each level: > tapply(X,X,length). > > Now I have another factor Y, which formed a matrix with X: > > X| A B B C A C B A C C > Y| B B C C C A A A B B > > I wanna count the number of elements for each of these > conditions: when X=A > and Y=A; when X=A and Y=B; when X=A and Y=C; when X=B and > Y=A; when X=B and > Y=B; when X=B and Y=C; when X=C and Y=A; when X=C and Y=B; > when X=C and > Y=C. > > The code I have written for this task is too complicated, > involving a lot > of for loops and if conditions. I believe there's some nice > code that can > do it far more efficiently. Can anyone give me a hint? > > Thanks a lot! > >
try this:
X <- factor(sample(c("A", "B", "C"), 10,
TRUE), levels = c("A", "B",
"C"))
Y <- factor(sample(c("A", "B", "C"), 10,
TRUE), levels = c("A", "B",
"C"))
#############
table(X, Y)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "zhihua li" <lzhtom at hotmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, June 21, 2005 2:46 PM
Subject: [R] how to count "associated" factors?
> hi netters
>
> Suppose I have a factor X, with 10 elements and 3 levels: A B B C A
> C B A C
> C .
>
> It is easy to count the number of elements for each level:
> tapply(X,X,length).
>
> Now I have another factor Y, which formed a matrix with X:
>
> X| A B B C A C B A C C
> Y| B B C C C A A A B B
>
> I wanna count the number of elements for each of these conditions:
> when X=A
> and Y=A; when X=A and Y=B; when X=A and Y=C; when X=B and Y=A; when
> X=B and
> Y=B; when X=B and Y=C; when X=C and Y=A; when X=C and Y=B; when X=C
> and
> Y=C.
>
> The code I have written for this task is too complicated, involving
> a lot
> of for loops and if conditions. I believe there's some nice code
> that can
> do it far more efficiently. Can anyone give me a hint?
>
> Thanks a lot!
>
>
--------------------------------------------------------------------------------
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
Reasonably Related Threads
- quotient and remainder
- Comparison of aggregate in R and group by in mysql
- An R clause to bind dataframes under certain contions
- Error: evaluation nested too deeply when doing heatmap with binary distfunction
- how to subset rows using regular expression patterns