我有一个数据框,我想保留任何价格列大于某个值的行(水果)。
这是一个可重现的示例,您可以将其直接复制并粘贴到 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 的水果,在本例中为香蕉和浆果
我期望的输出是以下两行:
banana 4563 10 9 11
berry 3235 20 2 8
我尝试过这样的事情:
output = df[which(df[,3:5] > price_threshold),]
但它不起作用。
这接近于这个帖子 https://stackoverflow.com/questions/21013198/return-row-numbers-for-a-particular-value-in-a-column-in-a-dataframe,但在这里我想查看最后三列中的任何值,而不仅仅是一列。
有什么建议吗?