我正在处理一个数据集,该数据集设置为增加行的6列设置。标题是
Brand, Channel, Date, Metric A, Metric B, B/A
示例数据:
Brands <- c("Brand A","Brand B","Brand C")
Channels <- (c("Channel A","Channel B","Channel A"))
MetricA <- c(1:3)
MetricB <- c(1:3)
df <- data.frame(Brands,Channels,MetricA,MetricB)
B/A <- df$MetricB/df$MetricA
df <- cbind(B/A)
我很想知道B/A
如果对Brand == Brand A AND Channel == Channel B
列进行平均分析。
答案 0 :(得分:3)
我已将此更改为品牌A和渠道A,因为您的示例数据中存在这些内容:
df$BdivA <- df$MetricB/df$MetricA
df_sub <- subset(df, Brands=='Brand A' & Channels=='Channel A')
mean(df_sub$BdivA)
如果你打算做更复杂的事情,那么dplyr软件包很棒。您可以使用它来计算品牌和渠道的所有组合的均值,如下所示:
df %>%
mutate(BdivA = MetricB/MetricA) %>%
group_by(Brands, Channels) %>%
summarize(mean_BdivA = mean(BdivA))