首页 > 解决方案 > 从一列中选择一个值过滤另一列

问题描述

我正在处理一个大数据项目,我想过滤一列而不是另一列突出显示。

例如,我想展示房屋 1,因此,我想将房屋 1 与其他 URBAN 房屋的其他价值进行比较,而不是所有房屋。

table <- data.frame(
  house=paste("House", 1:15),
  category = c("Urban", "Rural", "Suburban")
)
table
#       house category
# 1   House 1    Urban
# 2   House 2    Rural
# 3   House 3 Suburban
# 4   House 4    Urban
# 5   House 5    Rural
# 6   House 6 Suburban
# 7   House 7    Urban
# 8   House 8    Rural
# 9   House 9 Suburban
# 10 House 10    Urban
# 11 House 11    Rural
# 12 House 12 Suburban
# 13 House 13    Urban
# 14 House 14    Rural
# 15 House 15 Suburban

我试图试一试,但它对我不起作用......

table %>%
filter(house == house1) %>%
filter(category == table$house)

我希望输出看起来像这样......

#      house category
# 1  House 1    Urban
# 2  House 4    Urban
# 3  House 7    Urban
# 4 House 10    Urban
# 5 House 13    Urban

任何建议都非常感谢。

标签: rdplyr

解决方案


也许您可以尝试下面的基本 R 代码

subset(df,category == category[house=="House1"])

dplyr选项

df %>%
   filter(category == category[house == "House1"])

这使

     house category
1   House1    Urban
3   House3    Urban
5   House5    Urban
12 House12    Urban
13 House13    Urban
14 House14    Urban

虚拟数据

df <- structure(list(house = c("House1", "House2", "House3", "House4", 
"House5", "House6", "House7", "House8", "House9", "House10",
"House11", "House12", "House13", "House14", "House15"), category = c("Urban", 
"Suburban", "Urban", "Rural", "Urban", "Suburban", "Suburban",
"Rural", "Rural", "Suburban", "Suburban", "Urban", "Urban", "Urban",
"Rural")), class = "data.frame", row.names = c(NA, -15L))

推荐阅读