如何计算"average sales share"
How to calculate "average sales share"
我的数据涉及一家公司,包括总销售额和 CA、TX 和 WI 三个县的销售额。
数据:
> dput(head(WalData))
structure(list(CA = c(11047, 9925, 11322, 12251, 16610, 14696
), TX = c(7381, 5912, 9006, 6226, 9440, 9376), WI = c(6984, 3309,
8883, 9533, 11882, 8664), Total = c(25412, 19146, 29211, 28010,
37932, 32736), date = structure(c(1296518400, 1296604800, 1296691200,
1296777600, 1296864000, 1296950400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), event_type = c("NA", "NA", "NA", "NA", "NA", "Sporting"
), snap_CA = c(1, 1, 1, 1, 1, 1), snap_TX = c(1, 0, 1, 0, 1,
1), snap_WI = c(0, 1, 1, 0, 1, 1)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
我正在努力计算这三个州在公司总销售额中的平均销售额份额。
此外,我必须计算每年、一年中的月份和星期中的相同平均百分比。
任何建议都会很有帮助!
如果您以长格式获取数据,则执行所有计算会更容易。
library(dplyr)
library(tidyr)
WalData %>% pivot_longer(cols = CA:WI) %>% mutate(perc = value/Total)
使用 dplyr
您还可以尝试下一个选项。对于平均销售额,您可以使用下一个代码:
library(dplyr)
#Code 1
AvgSales <- WalData %>% select(c(CA,TX,WI)) %>%
summarise_all(mean,na.rm=T)
输出:
# A tibble: 1 x 3
CA TX WI
<dbl> <dbl> <dbl>
1 12642. 7890. 8209.
对于百分比,您需要根据 Total
:
计算比率
#Code 2
AvgSalesPerc <- WalData %>% select(c(CA,TX,WI,Total)) %>%
rowwise() %>% mutate(across(CA:WI,~./Total)) %>%
select(-Total) %>% ungroup() %>%
summarise_all(mean,na.rm=T)
输出:
# A tibble: 1 x 3
CA TX WI
<dbl> <dbl> <dbl>
1 0.444 0.278 0.278
在年月日的情况下,您可以从日期变量中提取值,然后使用 group_by()
并获取摘要。我只会做一年,因为它很容易延长月和日:
#Code 3 only year avg sales
AvgSalesYear <- WalData %>% mutate(Year=format(date,'%Y')) %>%
select(c(CA,TX,WI,Year)) %>%
group_by(Year) %>%
summarise_all(mean,na.rm=T)
输出:
# A tibble: 1 x 4
Year CA TX WI
<chr> <dbl> <dbl> <dbl>
1 2011 12642. 7890. 8209.
年级百分比的相同逻辑:
#Code 4 only year avg sales percentage
AvgSalesPercYear <- WalData %>% mutate(Year=format(date,'%Y')) %>%
select(c(CA,TX,WI,Year,Total)) %>%
rowwise() %>% mutate(across(CA:WI,~./Total)) %>%
select(-Total) %>%
group_by(Year) %>%
summarise_all(mean,na.rm=T)
输出:
# A tibble: 1 x 4
Year CA TX WI
<chr> <dbl> <dbl> <dbl>
1 2011 0.444 0.278 0.278
我们可以使用data.table
library(data.table)
melt(setDT(WalData), measure = c("CA", "TX", "WI"))[, perc := value/Total][]
我的数据涉及一家公司,包括总销售额和 CA、TX 和 WI 三个县的销售额。
数据:
> dput(head(WalData))
structure(list(CA = c(11047, 9925, 11322, 12251, 16610, 14696
), TX = c(7381, 5912, 9006, 6226, 9440, 9376), WI = c(6984, 3309,
8883, 9533, 11882, 8664), Total = c(25412, 19146, 29211, 28010,
37932, 32736), date = structure(c(1296518400, 1296604800, 1296691200,
1296777600, 1296864000, 1296950400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), event_type = c("NA", "NA", "NA", "NA", "NA", "Sporting"
), snap_CA = c(1, 1, 1, 1, 1, 1), snap_TX = c(1, 0, 1, 0, 1,
1), snap_WI = c(0, 1, 1, 0, 1, 1)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
我正在努力计算这三个州在公司总销售额中的平均销售额份额。
此外,我必须计算每年、一年中的月份和星期中的相同平均百分比。
任何建议都会很有帮助!
如果您以长格式获取数据,则执行所有计算会更容易。
library(dplyr)
library(tidyr)
WalData %>% pivot_longer(cols = CA:WI) %>% mutate(perc = value/Total)
使用 dplyr
您还可以尝试下一个选项。对于平均销售额,您可以使用下一个代码:
library(dplyr)
#Code 1
AvgSales <- WalData %>% select(c(CA,TX,WI)) %>%
summarise_all(mean,na.rm=T)
输出:
# A tibble: 1 x 3
CA TX WI
<dbl> <dbl> <dbl>
1 12642. 7890. 8209.
对于百分比,您需要根据 Total
:
#Code 2
AvgSalesPerc <- WalData %>% select(c(CA,TX,WI,Total)) %>%
rowwise() %>% mutate(across(CA:WI,~./Total)) %>%
select(-Total) %>% ungroup() %>%
summarise_all(mean,na.rm=T)
输出:
# A tibble: 1 x 3
CA TX WI
<dbl> <dbl> <dbl>
1 0.444 0.278 0.278
在年月日的情况下,您可以从日期变量中提取值,然后使用 group_by()
并获取摘要。我只会做一年,因为它很容易延长月和日:
#Code 3 only year avg sales
AvgSalesYear <- WalData %>% mutate(Year=format(date,'%Y')) %>%
select(c(CA,TX,WI,Year)) %>%
group_by(Year) %>%
summarise_all(mean,na.rm=T)
输出:
# A tibble: 1 x 4
Year CA TX WI
<chr> <dbl> <dbl> <dbl>
1 2011 12642. 7890. 8209.
年级百分比的相同逻辑:
#Code 4 only year avg sales percentage
AvgSalesPercYear <- WalData %>% mutate(Year=format(date,'%Y')) %>%
select(c(CA,TX,WI,Year,Total)) %>%
rowwise() %>% mutate(across(CA:WI,~./Total)) %>%
select(-Total) %>%
group_by(Year) %>%
summarise_all(mean,na.rm=T)
输出:
# A tibble: 1 x 4
Year CA TX WI
<chr> <dbl> <dbl> <dbl>
1 2011 0.444 0.278 0.278
我们可以使用data.table
library(data.table)
melt(setDT(WalData), measure = c("CA", "TX", "WI"))[, perc := value/Total][]