r - 从一列中选择一个值过滤另一列
问题描述
我正在处理一个大数据项目,我想过滤一列而不是另一列突出显示。
例如,我想展示房屋 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
任何建议都非常感谢。
解决方案
也许您可以尝试下面的基本 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))
推荐阅读
- python - 如何在不实际键入变量的情况下删除多个变量?
- python - 从具有重复数据的行中创建列中的数组
- heroku - 在heroku上部署的MERN应用程序路径中的问题
- swift - 如何在 Swift 中表示 64 位整数?
- javascript - 在 ng-repeat 中显示隐藏输入
- apple-tv - Apple TV 中的 TVJS 播放器在 tvOS14 测试版中误触发事件 mediaItemDidChange
- javascript - 有没有办法在 react-native 中重用组件?
- react-native - 如何在导航 v5 createbottomtabNavigator 中禁用 tabBarBadge?
- java - Mockito - 模拟 ApplicationContext
- c++ - Boost ASIO segfault due to socket no longer existing after a second connection is accepted