在我的数据集(比如dat1)中,我有一个名为Campaign_Response的分类变量,其中包含值False和True。同样在同一个数据集中,我为Age变量创建了存储桶。这个age_bucket变量的值包括Teen,Adult和Senior。 现在我想通过Age_bucket变量计算广告系列响应率。我开始使用dplyr函数来解决这个问题:
dat1%>%group_by(Age_bucket)%>%summarize(ResponseRate= ???)
我有点陷入总结部分,因为Campaign_response是一个分类变量,我不知道如何继续这个。 你能帮我看看年龄段的广告系列回复率吗?感谢您的所有帮助。感谢。
答案 0 :(得分:1)
确保Campaign_Response是逻辑向量,然后使用
Linq
答案 1 :(得分:0)
# data generation
exempleData <- data.frame(order = 1:100 ,age_group = sample(c("A1","A2","A3"),100 ,replace = T),Response = sample(c(T,F),100 ,replace = T))
#compute rates
library(plyr)
rate <- ddply(exempleData,
.(age_group),
summarise,
yes_rate = length(Response[Response == T]) / length(Response),
.parallel = F )
# display rates
rate
答案 2 :(得分:0)
或者如果你想要检查两者之间的步骤,你可以做这样的事情。
#some data made
Campaign_Resp <- c(T, F, T, T, F, T, F, T,F, T, T, F, T, F)
dat1 <- as.data.frame(Campaign_Resp)
Age <- c("Teen", "Teen", "Adult","Teen", "Adult","Adult", "Senior", "Teen", "Adult", "Adult", "Senior", "Adult", "Senior", "Senior")
dat1$Age <- Age
#transforming datatype
dat1$Campaign_Resp <- as.numeric(dat1$Campaign_Resp)
#using dplyr to count how many times a person inside a agegroup awnsered or not
dat2<- dat1%>%group_by(Age, Campaign_Resp) %>% summarise(Aantal= n())
dat3<- dat2 %>% spread(Campaign_Resp,value = Aantal)
#calculate non-responsive and responsive percentage
dat3$TOT <- dat3$`0`+ dat3$`1`
dat3$resp <- dat3$`1`/ dat3$TOT
dat3$nonresp <- dat3$`0`/ dat3$TOT
答案 3 :(得分:-1)
这个怎么样:
df <- data.frame(Campaign_Response = c(TRUE, FALSE, TRUE, FALSE, FALSE, TRUE),
Age_Bucket = c("teen", "adult", "senior", "teen", "adult", "senior"))
df %>%
group_by(Age_Bucket, Campaign_Response) %>%
summarize(ResponseRate = n())
如果你想知道每个年龄组你有多少TRUE值,你可以这样做:
df %>%
group_by(Age_Bucket) %>%
summarize(ResponseRate= sum(Campaign_Response))