Using loop and conditional statements, I would like to identify rows that have a value over 2.50
customer <- c("john", "amy", "doug")
product <- c("coffee", "pastry", "pastry")
store <- c("a", "b", "c")
cost <- c(5.50, 2.45, 3.00)
df <- data.frame(customer, product, store, cost)
I would like to identify purchases over $2.50 and save "store" and "product" as separate vectors associated with those purchases over $2.50.
So far, this is my code and it is not working...
for (row in 1:nrow(df)) {
customer <- df[row, "customer"]
product <- df[row, "product"]
store <- df[row, "store"]
cost <- df[row, "cost"]
if(cost > 2.50) {
print(paste(customer, "purchased", product, "at", store, "for", cost))
}
}
This is not for working, and how do I save the two "product" and "store" as separate vectors?