我想要一个交叉表来找到两个变量之间的响应率
I want a cross tab to find the response rate between two variables
考虑这个名为 Survey 的数据集,它有 3 列(Age_Group、Tenure 和 Campaign_Response):
ID<- seq(1:10)
Age_Group<-sample(c("Teen", "Adult", "Senior"), 10, replace = TRUE)
Tenure<-sample(c(1,2,3,4,5), 10, replace = TRUE)
Campaign_Response<-sample(c(T, F), 10, replace = TRUE)
Survey<-data.frame(Age_Group, Tenure, Campaign_Response)
我现在想创建 age_group 和客户保留期之间的活动响应率的交叉表。您能否为此建议代码。感谢您的帮助。
使用 xtabs
获取活动响应率与任期和年龄的交叉表。
xtabs(Campaign_Response ~ Age_Group+Tenure, data = Survey)
# Tenure
# Age_Group 1 2 3 4 5
# Adult 1 0 0 0 1
# Senior 0 1 1 1 0
# Teen 0 0 0 2 0
数据:
Survey <- structure(list(Age_Group = structure(c(3L, 1L, 1L, 2L, 3L, 2L, 2L, 1L, 1L, 3L),
.Label = c("Adult", "Senior", "Teen"),
class = "factor"),
Tenure = c(2, 1, 4, 2, 4, 3, 4, 5, 2, 4),
Campaign_Response = c(FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE)),
.Names = c("Age_Group", "Tenure", "Campaign_Response"),
row.names = c(NA, -10L), class = "data.frame")
我们可以使用 acast
来自 reshape2
library(reshape2)
acast(Survey, Age_Group~Tenure, sum)
# 1 2 3 4 5
#Adult 1 0 0 0 1
#Senior 0 1 1 1 0
#Teen 0 0 0 2 0
考虑这个名为 Survey 的数据集,它有 3 列(Age_Group、Tenure 和 Campaign_Response):
ID<- seq(1:10)
Age_Group<-sample(c("Teen", "Adult", "Senior"), 10, replace = TRUE)
Tenure<-sample(c(1,2,3,4,5), 10, replace = TRUE)
Campaign_Response<-sample(c(T, F), 10, replace = TRUE)
Survey<-data.frame(Age_Group, Tenure, Campaign_Response)
我现在想创建 age_group 和客户保留期之间的活动响应率的交叉表。您能否为此建议代码。感谢您的帮助。
使用 xtabs
获取活动响应率与任期和年龄的交叉表。
xtabs(Campaign_Response ~ Age_Group+Tenure, data = Survey)
# Tenure
# Age_Group 1 2 3 4 5
# Adult 1 0 0 0 1
# Senior 0 1 1 1 0
# Teen 0 0 0 2 0
数据:
Survey <- structure(list(Age_Group = structure(c(3L, 1L, 1L, 2L, 3L, 2L, 2L, 1L, 1L, 3L),
.Label = c("Adult", "Senior", "Teen"),
class = "factor"),
Tenure = c(2, 1, 4, 2, 4, 3, 4, 5, 2, 4),
Campaign_Response = c(FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE)),
.Names = c("Age_Group", "Tenure", "Campaign_Response"),
row.names = c(NA, -10L), class = "data.frame")
我们可以使用 acast
来自 reshape2
library(reshape2)
acast(Survey, Age_Group~Tenure, sum)
# 1 2 3 4 5
#Adult 1 0 0 0 1
#Senior 0 1 1 1 0
#Teen 0 0 0 2 0