2
votes

I have the following data frame in R which presents the number of purchases per year over a period of four years, for 4 customers. The member_since variable shows the year when the customer joined the company.

id<-c(1,2,3,4)
member_since<-c(2014,2016,2015,2014)
X2014<-c(2,0,0,3)
X2015<-c(3,0,4,2)
X2016<-c(3,2,3,4)
X2017<-c(2,3,6,0)
df<-data.frame(id,member_since,X2014,X2015,X2016,X2017)

 id    member_since X2014 X2015 X2016 X2017
 1         2014      2     3     3     2
 2         2016      0     0     2     3
 3         2015      0     4     3     6
 4         2014      3     2     4     0

Now I am trying to create a new variable mean_purchase to calculate the average number of purchases per year for each customer, from the year s/he joined. This means for example for customer 2 the total number of purchases should be divided by 2, but for customer 4 the sum of purchases should be divided by 4.

 id    member_since X2014 X2015 X2016 X2017 mean_purchase
 1         2014      2     3     3     2         2.5
 2         2016      0     0     2     3         2.5
 3         2015      0     4     3     6         4.33
 4         2014      3     2     4     0         2.25

Would appreciate your help on this.

5

5 Answers

4
votes

We can use apply

df$mean_purchase <- apply(df[3:6], 1, function(x) round(sum(x)/sum(cumsum(x > 0) > 0), 2))
df$mean_purchase
#[1] 2.50 2.50 4.33 2.25

Or use rowCumsums from matrixStats

library(matrixStats)
rowSums(df[3:6])/rowSums(rowCumsums(+(df[3:6] > 0)) > 0)
2
votes

Based on akrun's answer, here is a slight generalization if you want to include cases where customer joined the program but did not buy anything on the first year(s), using mapply.

mapply(function(x,y) round(mean(unlist(df[x,(3+y):6])),2), 
       1:nrow(df), df$member_since - 2014)
# [1] 2.50 2.50 4.33 2.25
2
votes

A different solution with apply

df$mean_purchase <- apply(df[3:6], 1, function(x) mean(x[min(which(x != 0)):length(x)]))
df$mean_purchase
# [1] 2.500000 2.500000 4.333333 2.250000
0
votes

We replace by NA the irrelevant zeroes (and I suggest you keep them this way), then we use rowMeans.

df[-(1:2)][t(apply(df[-(1:2)],1,cumsum))==0] <- NA
df$mean_purchase <- rowMeans(df[-(1:2)],na.rm=T)

#   id member_since X2014 X2015 X2016 X2017 mean_purchase
# 1  1         2014     2     3     3     2      2.500000
# 2  2         2016    NA    NA     2     3      2.500000
# 3  3         2015    NA     4     3     6      4.333333
# 4  4         2014     3     2     4     0      2.250000

# If you really don't want to keep NAs :
df[is.na(df)] <- 0 
0
votes

An option is to use tidyr::gather and dplyr::filter to ensure that mean is calculated only for the years after member_since. The implementation can be as:

library(tidyverse)

df %>% gather(year, value, -id, -member_since) %>%
  filter(member_since <= as.numeric(gsub("^X(\\d+)","\\1",year))) %>%
  group_by(id) %>%
  summarise(mean = mean(value)) %>%
  right_join(df, by="id") %>%
  select(-mean, mean) %>% as.data.frame()

#   id member_since X2014 X2015 X2016 X2017 mean
# 1  1         2014     2     3     3     2 2.50
# 2  2         2016     0     0     2     3 2.50
# 3  3         2015     0     4     3     6 4.33
# 4  4         2014     3     2     4     0 2.25