[英]Having difficulty obtaining subset in R
我正在嘗試對具有以下要求的數據集進行子集化:
ethnicity
是xyz
education
是學士學位及以上,即Bachelor's Degree
或Graduate Degree
$30,000 - $39,999
或$100,000 - $124,999
$30,000 - $39,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步。
library(dplyr)
df %>%
filter(ethnicity == "xyz" &
education %in% c("Bachelor's Degree", "Graduate Degree")) %>%
group_by(religious) %>%
summarize(lower_bound = min(income),
upper_bound = max(income) )
更改我的評論的時間過長。
首先,您的代碼已經差不多了; 由於收入是向量而不是數據框,因此不需要結尾逗號。 即你可以使用
df$income[which(df$ethnicity == "xyz" &
df$education %in% c("Bachelor's Degree", "Graduate Degree") ]
# note no comma after the closing bracket
如果要創建子集數據,則一開始不要包含df$income
,只需使用df
並保持逗號即可。 這將子集您的數據,但保留所有列
sub_df <- df[which(df$ethnicity == "xyz" &
df$education %in% c("Bachelor's Degree", "Graduate Degree"), ]
要查看子數據的income
水平,可以使用table
table(sub_df$income)
您可以再次使用table
檢查按religious
狀況table
的每項income
的觀察數。
table(sub_df$income, sub_df$religious)
如果您只想選擇income
和religious
欄,也可以使用[
sub_df[c("religious", "income")]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.