我試圖子集以下要求的數據集合獲得子集:有困難中的R
ethnicity
是xyz
education
是本科及以上學歷,即Bachelor's Degree
或Graduate Degree
- 我然後想看看符合上述要求的人的收入狀況。括號可以是
$30,000 - $39,999
或$100,000 - $124,999
。 - 最後,作爲我的最終結果,我想看看從第三個項目(上面)獲得的子集與這些人是否是宗教的列。在數據集中,對應於
religious
和not religious
。
因此,這將是這個樣子
income religious
$30,000 - $39,999 not religious
$50,000 - $59,999 religious
.... ....
.... ....
保持頭腦列出的那些滿足條件1和2
請記住,我是新來編程。我試圖弄清楚很長一段時間,並已經挖掘了很多帖子。我似乎無法得到任何工作。我該如何解決?有人請幫忙。
以便不採取從崗位的清晰了,我會寄我已經試過以下(但隨時忽略它,因爲它可能是垃圾)。
我曾嘗試只是爲了得到第3步以下的許多變化,但都遭到慘敗,而我即將與鍵盤來砸我的頭:
df$income[which(df$ethnicity == "xyz" & df$education %in% c("Bachelor's Degree", "Graduate Degree"), ]
我也試過:
race <- df$ethnicity == "xyz"
ba_ma_phd <- df$education %in% c("Graduate Degree", "Bachelor's Degree")
income_sub <- df$income[ba_ma_phd & race]
我相信income_sub
讓我到步驟3,但我不知道如何得到它的步驟4
你幾乎沒有;因爲收入是一個矢量而不是數據框,所以你不需要尾隨的逗號。即你可以使用'df $ income ['%d'(df $ ethnicity ==「xyz」&df $ education%in%c(「Bachelor's Degree」,「Graduate Degree」)]'注意,如果種族或教育缺失,你可能希望在你的子集聲明中包含非缺失變量(如果你想創建一個子集數據,那麼在開始時不要包括'df $ income',只需使用'df'並保留這個逗號, ...所以'sub_df < - df [其中(df $種族==「xyz」&df $ education%in%c(「學士學位」,「研究生學位」)]' – user20650
@ user20650那麼我該如何獲得對應的列'宗教'? – AlanH
我有點不清楚你想要什麼...只是這可能是'表(sub_df $收入,sub_df $宗教)'還是你想要全列'sub_df [c(「收入」, 「宗教」)]' – user20650