0

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?

steveb
  • 5,382
  • 2
  • 27
  • 36
Tawk_Tomahawk
  • 139
  • 2
  • 8

3 Answers3

0

There is no need for an explicit for loop.

This saves columns store and product separately, for which cost > 2.5:

store.sel <- df[which(df$cost > 2.5), "store"]
product.sel <- df[which(df$cost > 2.5), "product"]

Or subset the dataframe

subset(df, cost > 2.5)

and then select the required columns

with(subset(df, cost > 2.5), paste(customer, "purchased", product, "at", store, "for", cost))
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • You don't need to use `which` : `df[df$cost > 2.5, "store"]` gives same result as `df[which(df$cost > 2.5), "store"]` – HubertL Oct 10 '17 at 01:25
  • True. But it doesn't hurt either. In my opinion, returning indices has advantages for more complex filtering, and I consider it good (better) practice. See e.g. the discussion [here](https://stackoverflow.com/questions/6918657/whats-the-use-of-which). – Maurits Evers Oct 10 '17 at 01:27
0

You could do the following which outputs a vector of your strings of interest:

df2 <- df[df$cost > 2.5,]
with(df2, paste(customer, "purchased", product, "at", store, "for", cost))

## [1] "john purchased coffee at a for 5.5" "doug purchased pastry at c for 3"
steveb
  • 5,382
  • 2
  • 27
  • 36
  • Thank you. This works! Is this feasible with a for loop and if conditional statement? – Tawk_Tomahawk Oct 10 '17 at 05:47
  • Yes, but often such operations are more efficient and more readable when not using a `for` loop. For future needs, you may want to look at `dplyr` (actually `tidyverse`) and / or `data.table`; these are quite efficient and avoid the need for most `for` loops. These weren't necessary for this question though. – steveb Oct 10 '17 at 14:43
0

I'm not sure why you are saving, but you could do something like this.

   df <- df[df$cost > 2.50,]
   cat(paste0(df$customer, " purchased ", df$product, 
       " at ",df$store, " for ", cost, "./n"))
Elin
  • 6,507
  • 3
  • 25
  • 47