gpt4 book ai didi

r - 从一列中选择一个值过滤另一列

转载 作者:行者123 更新时间:2023-12-04 01:15:27 25 4
gpt4 key购买 nike

我正在处理一个大数据项目,我想过滤一列以防止另一列突出显示。

例如,我想展示房屋 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))

关于r - 从一列中选择一个值过滤另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63513439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com