如果任何特定列集中的值满足特定条件,则返回整行 [英] Return an entire row if the value in any specific set of columns meets a certain criteria
问题描述
我有一个数据框,我想保留其中任何价格列大于某个值的行(水果)。
I have a dataframe, and I want to retain the rows (fruits) for which any of the price columns is greater than a certain value.
这里是一个您可以直接将其复制并粘贴到R中的可复制示例:
Here is a reproducible example that you can copy&paste directly into R:
fruit = c("apple","orange","banana","berry") #1st col
ID = c(123,3453,4563,3235) #2nd col
price1 = c(3,5,10,20) #3rd col
price2 = c(5,7,9,2) #4th col
price3 = c(4,1,11,8) #5th col
df = as.data.frame(cbind(fruit,ID,price1,price2,price3)) #combine into a dataframe
price_threshold = 10 #define a price
我只想获取任何价格大于10的水果,在这种情况下为香蕉和浆果
I want to get only the fruits for which any of the prices is greater than 10, which are Banana and Berry in this case
我期望的输出是以下两行:
The output I'm expecting is the following two rows:
banana 4563 10 9 11
berry 3235 20 2 8
我尝试过类似的操作:
output = df[which(df[,3:5] > price_threshold),]
但没有用。
这接近这篇文章,但在这里我想看看最后三列中的任何值,而不仅仅是一列。
this is close to this post, but here I want to look at any of the values in the last three columns, not just one column.
有什么建议吗?
推荐答案
首先,最好将data.frame初始化为
First, it is better to initialize your data.frame as
df = data.frame(fruit,ID,price1,price2,price3)
因此变量未解析为因数。然后,您可以通过以下方式获得预期的结果:
So the variables are not parsed to factors. You can then get your expected outcome with:
df[rowSums(df[,3:5] > price_threshold)>0,]
结果:
fruit ID price1 price2 price3
3 banana 4563 10 9 11
4 berry 3235 20 2 8
希望这会有所帮助!
这篇关于如果任何特定列集中的值满足特定条件,则返回整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!