[英]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.