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.
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
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
I tried something like this:
output = df[which(df[,3:5] > price_threshold),]
but it didn't work.
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.
Any suggestion?