Question

I have a data frame as follows:

> head(data1)
  Age Gender Impressions Clicks Signed_In    agecat  scode
1  36      0           3      0         1   (34,44]   Imps
2  73      1           3      0         1 (64, Inf]   Imps
3  30      0           3      0         1   (24,34]   Imps
4  49      1           3      0         1   (44,54]   Imps
5  47      1          11      0         1   (44,54]   Imps
6  47      0          11      1         1   (44,54] Clicks

Str Info:

> str(data1)
'data.frame':   458441 obs. of  7 variables:
 $ Age        : int  36 73 30 49 47 47 0 46 16 52 ...
 $ Gender     : int  0 1 0 1 1 0 0 0 0 0 ...
 $ Impressions: int  3 3 3 3 11 11 7 5 3 4 ...
 $ Clicks     : int  0 0 0 0 0 1 1 0 0 0 ...
 $ Signed_In  : int  1 1 1 1 1 1 0 1 1 1 ...
 $ agecat     : Factor w/ 8 levels "(-Inf,0]","(0,18]",..: 5 8 4 6 6 6 1 6 2 6 ...
 $ scode      : Factor w/ 3 levels "Clicks","Imps",..: 2 2 2 2 2 1 1 2 2 2 ...
>

For each row like to calculate the Click Through Rate(CTR) which is defined as (Clicks/Impressions)*100.

I'd like to get the average CTR for each gender in each category. Something like:

Gender 0, Category (0,18] CTR = ??.
Gender 1, Category (0,18] CTR = ??.
Gender 0, Category (18,24] CTR = ??.
Gender 1, Category (18,24] CTR = ??.
and so on...

How do I go about achieving this in R language?

Some of the stuff I tried for initially grouping by Gender:

> calcCTR <- function(var1,var2){
+   (var1*100)/var2
+ }

And called it using summaryBy

> summaryBy(Clicks~Gender, data=data1, FUN=calcCTR, var2=data1$Impressions)

which took inexplicably long time.

Another approach:

> summaryBy(((Clicks*100)/Impressions)~Gender, data=data1, FUN=sum)
  Gender ((Clicks * 100)/Impressions).sum
1      0                              NaN
2      1                              NaN
>

I also added a column CTR to the data:

> data1$ctr = (data1$Clicks/data1$Impressions)*100
> head(data1)
  Age Gender Impressions Clicks Signed_In    agecat  scode      ctr
1  36      0           3      0         1   (34,44]   Imps 0.000000
2  73      1           3      0         1 (64, Inf]   Imps 0.000000
3  30      0           3      0         1   (24,34]   Imps 0.000000
4  49      1           3      0         1   (44,54]   Imps 0.000000
5  47      1          11      0         1   (44,54]   Imps 0.000000
6  47      0          11      1         1   (44,54] Clicks 9.090909
>

However, when I stratify it by gender or age, it gives me NaN.

> summaryBy(ctr~agecat,
+ data=data1);
     agecat ctr.mean
1  (-Inf,0]      NaN
2    (0,18]      NaN
3   (18,24]      NaN
4   (24,34]      NaN
5   (34,44]      NaN
6   (44,54]      NaN
7   (54,64]      NaN
8 (64, Inf]      NaN
> summaryBy(ctr~Gender,
+ data=data1);
  Gender ctr.mean
1      0      NaN
2      1      NaN
>
Was it helpful?

Solution

This should get you going:

library(data.table)
dt = as.data.table(data1)

dt[, mean((Clicks/Impressions)*100), by = list(Gender, agecat)]

OTHER TIPS

This trivial example should help you get started

#create our trivial data set
dat<-data.frame(c1=rep(c("a","b"),each=2),c2=rep(1:2,2),val=rnorm(4))
#look into learning about tapply, lapply, apply, sapply, 
tapply(dat$val, list(dat$c1,dat$c2),mean)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top