# Calculate average by group

set.seed(nchar(first_name)+nchar(last_name))
n.pop<-10000
subscribe<-sample(c(1,0),n.pop,replace=TRUE,prob=c(0.5,0.5))
pb1<-sample(65:75,1)/100
pb2<-0.5
ap1<-sample(65:75,1)/100
ap2<-0.5
set.seed(nchar(last_name))
rm(list = ls(pattern="[^data,first_name,last_name]"))

#the above is my data and I am trying to calculate the average rate for the group that sees the ad and for the ones who don't

# I have the code below

sapply(df, mean)

but I am not sure if I am doing this correct, any comments?

See the FAQ: How to do a minimal reproducible example `reprex` for beginners. Most of the pieces are here, but some glitches exist, such as

that requires reverse engineering to address the problems in the terms posed. A `reprex` has the advantage of running "as-is" on another's RStudio session.

Couple of pointers before getting to an example using simpler data

1. Use snake_case rather than dotted.separators as a matter of good style
2. don't name objects `df`, `data`, `date` or other words that are built-in functions or functions loaded by libraries; some operations give precedence to the function name
3. Anything in a Stats 101 textbook has a function already written. Instead of

use

``````subscribe <- rbinom(n=n_pop, size=1, prob=0.5)
``````
1. Construct data frames directly
``````DF <- data.frame(subscribe = subscribe, see_ad = see_ad, buy = buy)
``````

Here is fake data composed of binary outcomes illustrating contingency tables with count and with proportion results.

``````set.seed(42)
N <- 100
exposed <- rbinom(n=N, size=1, prob=0.25)
set.seed(137)
purchased <- rbinom(n=N, size=1, prob=0.05)
DF <- data.frame(exposed = as.factor(exposed),purchased = as.factor(purchased))
table(DF)
#>        purchased
#> exposed  0  1
#>       0 72  2
#>       1 25  1
table(DF)/N
#>        purchased
#> exposed    0    1
#>       0 0.72 0.02
#>       1 0.25 0.01
``````
1 Like

thanks for that this is what I came up with but again since I am new to r dont know if its correct

###########################################

This is still opaqueâ€”particularly without data. Questions that require reverse engineering the problem are far less likely to receive helpful answers than those with a cut-and-paste `reprex` described in the FAQ listed.