two-way factor

16 views
Skip to first unread message

Asaf Weinstein

unread,
Aug 25, 2014, 10:48:07 PM8/25/14
to israel-r-user-group
שלום לכולם,

מהי הדרך הפשוטה ביותר ליצור שני factors עבור טבלת ניתוח שונות דו-כוונית?
אני כרגע משתמש ב:

A.level <- factor( rep(1:R, apply(K,1,sum)))

B.level <- factor( unlist( apply(K,1,function(t) rep( 1:C, t )) ) )

אבל מנחש שיש דרך פשוטה יותר..
תודה!
אסף

Tal Galili

unread,
Aug 26, 2014, 5:59:17 AM8/26/14
to israel-r-...@googlegroups.com
היי אסף,
רק כדי לוודא שאני מבין את שאלתך, תוכל לשלוח קוד שהוא self contained, שאפשר לראות מה התוצאה הסופית שאתה מחפש להשיג?



----------------Contact Details:-------------------------------------------------------
Contact me: Tal.G...@gmail.com
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
----------------------------------------------------------------------------------------------



--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Asaf Weinstein

unread,
Aug 26, 2014, 1:45:18 PM8/26/14
to israel-r-user-group

הי טל,

סליחה - הנה קוד self contained:

R <- 4  # number of "row" factors

C <- 3  # number of "column" factors  

K <- matrix( sample(c(1,2),R*C,replace=T), nrow=R )  # K_ij is the number of observations in the ij cell

A.level <- factor( rep(1:R, apply(K,1,sum)) )

B.level <- factor( unlist( apply(K,1,function(t) rep( 1:C, t )) ) )

cbind(A.leve,B.level)  # this is the final result I'm looking for 


תודה!

אסף

Tal Galili

unread,
Aug 30, 2014, 9:00:15 AM8/30/14
to israel-r-...@googlegroups.com
היי אסף,
צר לי על המענה המעט מאוחר.
האם הפונקציה הבאה תעזור לך:
expand.grid(1:4, 1:3)
עוד פונקציה ששווה להכיר בכללי היא:
combn(1:4, 2)
האם זה עוזר?

טל



----------------Contact Details:-------------------------------------------------------
Contact me: Tal.G...@gmail.com
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
----------------------------------------------------------------------------------------------



amit gal

unread,
Aug 30, 2014, 5:51:41 PM8/30/14
to israel-r-...@googlegroups.com
הו... עכשיו אני חושב שאני מבין מה אתה מחפש. אני חושב שזה יעשה לך את העבודה (עד כדי מיון רצוי):

cbind( rep(row(K),K),rep(col(K),K) )

Tal Galili

unread,
Aug 31, 2014, 6:16:20 AM8/31/14
to israel-r-...@googlegroups.com
עמית - זה נראה יישום ממש מדליק ל row/col. (למרות שקצת מפחיד למצוא מה יביא בדיוק לצרכים של אסף איתו, אבל זה נראה אפשרי)
ולשאר המשתמשים שימו לב כיצד rep בעצם הופך את שני המטריצות K ו- row(K) להיות ווקטור, ואז עליהם הוא מפעיל את rep - חביב.





----------------Contact Details:-------------------------------------------------------
Contact me: Tal.G...@gmail.com
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English)
----------------------------------------------------------------------------------------------



Asaf Weinstein

unread,
Aug 31, 2014, 3:38:57 PM8/31/14
to israel-r-user-group
תודה טל ועמית!
עמית, מה שהצעת עובד (עד כדי הסדר, הביטוי הבא יחזיר בדיוק את מה שרציתי):
cbind(rep(t(row(K)),t(K)), rep( t(col(K)),t(K) ))

Asaf Weinstein

unread,
Aug 31, 2014, 3:39:50 PM8/31/14
to israel-r-user-group
סליחה, טעיתי בשורה האחרונה:
cbind(rep(t(row(K)),t(K)), rep( t(col(K)),t(K) ))
Reply all
Reply to author
Forward
0 new messages