1
想象我想創建一個列聯表的移動和性別爲具有以下數據集:R表xtab中:列聯表上的排除條件因素
df<-data.frame(read.table(header = TRUE, text =
"Age Gender Mobile
13 Male 1
14 Female 1
18 Female 1
13 Male 1
13 Female 1
14 Female 0
16 Male 1
16 Female 0
16 Male 0
20 Male 0
14 Male 0
13 Female 0
25 Male 1
13 Female 0
24 Female 0"))
我會去了解它,如下所示:
df$Gender<-factor(df$Gender)
df$Mobile<-factor(df$Mobile)
prop.table(xtabs(~Mobile+Gender, df))
它工作正常。現在,想象一下,我想爲18歲以上的樣本子集編制一個適合性別和移動性的表格。您如何做到這一點?我嘗試了以下無濟於事:
prop.table(xtabs(~Mobile+Gender, df[df$Age>18]))
或
prop.table(xtabs(~Mobile+Gender[df$Age>18], df))
或
prop.table(xtabs(~Mobile+Gender, df))[df$Age>18]
任何想法?謝謝!
!而且你是對的,夥計!感謝修正以及快速回復!乾杯 – DomB