1

I would like to insert a new column in a data.frame that counts how many times a client appeared so far. But, I would like it increase by 1 each time. So, the first purchase of client A is 1, the second is 2...

In Excel you proceed with a count.if function.

I've tried this two functions, but the first ten are zero.

customer <- as.factor(sample(c("A","B","C","D"), 50, replace = TRUE, prob = c(10,20,30,40)))
purchase <- round(runif(50, 50,150),0)
purch_id <- 1:50
my_store <- data.frame(customer,purchase, purch_id)
my_store$number1 <- apply(my_store, 1, function(x)length(which(my_store$customer==x[1] & my_store$purch_id<x[3])))
my_store$number2 <- apply(my_store, 1, function(x)sum((my_store$customer==x[1] & my_store$purch_id<x[3])))
my_store

The first column is the client, the second how much he spent and the third is a unique id of each purchase. And each row is a an unique purchase two.

Thank you!

  • For a reproducible example, please use `set.seed()` before calling `sample()` or `runif()`, etc. to ensure that the generated sample data will be the same for all. – Uwe Mar 18 '17 at 16:24

1 Answers1

0

This works...

my_store$number3 <- sapply(1:nrow(my_store),function(i) sum(my_store$customer==my_store$customer[i] & my_store$purch_id<=my_store$purch_id[i]))

> my_store
   customer purchase purch_id number1 number2 number3
1         B      100        1       0       0       1
2         C      120        2       0       0       1
3         D       57        3       0       0       1
4         C       98        4       0       0       2
5         C       92        5       0       0       3
6         C      127        6       0       0       4
7         B      136        7       0       0       2
8         C       87        8       0       0       5
9         D       56        9       0       0       2
10        B      114       10       1       1       3
11        B      128       11       2       2       4
12        B      142       12       3       3       5
13        B       76       13       4       4       6
14        C      138       14       0       0       6
15        B       71       15       5       5       7
16...
Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32