计算列中每个唯一值的平均值,并插入到 R 中等于 0 的列中
Calculate mean for each unique value in column and insert into the column where it equals 0 in R
我的例子的数据。
date1 = seq(as.Date("2019/01/01"), by = "month", length.out = 48)
date2 = seq(as.Date("2019/02/01"), by = "month", length.out = 48)
date3 = seq(as.Date("2019/02/01"), by = "month", length.out = 48)
date4 = seq(as.Date("2019/02/01"), by = "month", length.out = 48)
date = c(date1,date2,date3,date4)
subproducts1=rep("1",48)
subproducts2=rep("2",48)
subproductsx=rep("x",48)
subproductsy=rep("y",48)
b1 <- c(rnorm(48,5))
b2 <- c(rnorm(48,5))
b3 <-c(rnorm(48,5) )
b4 <- c(rnorm(48,5))
dfone <- data.frame(
"date"= date,
"subproduct"=
c(subproducts1,subproducts2,subproductsx,subproductsy),
"actuals"= c(b1,b2,b3,b4))
这会为日期 2、3、4 创建值为 0 的 2019 年 1 月。
dfone <-dfone %>%
complete(date = seq.Date(from = min(date), to = as.Date('2021-06-01'), by = 'month'),
nesting(subproduct), fill = list(actuals = 0))
问题:如何计算每个唯一子产品的平均值(在本例中为 4 个)并将平均值插入到每个相应的 2019 年 1 月创建的值 = 0 中?我知道我可以手动执行此操作,但是否有任何功能可以轻松执行此操作?
我们可以通过替换进行分组,即按 'subproduct' 分组,在 replace
中创建条件,其中 'actuals' 值为 0,然后用 mean
更新这些条件'actuals' 不包括 mutate
中的 0 值
library(dplyr)
dfone_new <- dfone %>%
group_by(subproduct) %>%
mutate(actuals = replace(actuals, actuals == 0,
mean(actuals[actuals != 0], na.rm = TRUE))) %>%
ungroup
或者在complete
中,如果我们不将其更改为0,则更直接地使用na.aggregate
从zoo
默认使用函数mean
替换 NA
个元素
library(tidyr)
library(zoo)
dfone_new <- dfone %>%
complete(date = seq.Date(from = min(date),
to = as.Date('2021-06-01'), by = 'month'),
nesting(subproduct)) %>%
group_by(subproduct) %>%
mutate(actuals = na.aggregate(actuals)) %>%
ungroup
-输出
dfone_new
# A tibble: 195 x 3
date subproduct actuals
<date> <chr> <dbl>
1 2019-01-01 1 5.67
2 2019-01-01 2 5.01
3 2019-01-01 x 5.00
4 2019-01-01 y 4.98
5 2019-02-01 1 3.97
6 2019-02-01 2 5.42
7 2019-02-01 x 5.09
8 2019-02-01 y 3.98
9 2019-03-01 1 5.18
10 2019-03-01 2 5.08
# … with 185 more rows
我的例子的数据。
date1 = seq(as.Date("2019/01/01"), by = "month", length.out = 48)
date2 = seq(as.Date("2019/02/01"), by = "month", length.out = 48)
date3 = seq(as.Date("2019/02/01"), by = "month", length.out = 48)
date4 = seq(as.Date("2019/02/01"), by = "month", length.out = 48)
date = c(date1,date2,date3,date4)
subproducts1=rep("1",48)
subproducts2=rep("2",48)
subproductsx=rep("x",48)
subproductsy=rep("y",48)
b1 <- c(rnorm(48,5))
b2 <- c(rnorm(48,5))
b3 <-c(rnorm(48,5) )
b4 <- c(rnorm(48,5))
dfone <- data.frame(
"date"= date,
"subproduct"=
c(subproducts1,subproducts2,subproductsx,subproductsy),
"actuals"= c(b1,b2,b3,b4))
这会为日期 2、3、4 创建值为 0 的 2019 年 1 月。
dfone <-dfone %>%
complete(date = seq.Date(from = min(date), to = as.Date('2021-06-01'), by = 'month'),
nesting(subproduct), fill = list(actuals = 0))
问题:如何计算每个唯一子产品的平均值(在本例中为 4 个)并将平均值插入到每个相应的 2019 年 1 月创建的值 = 0 中?我知道我可以手动执行此操作,但是否有任何功能可以轻松执行此操作?
我们可以通过替换进行分组,即按 'subproduct' 分组,在 replace
中创建条件,其中 'actuals' 值为 0,然后用 mean
更新这些条件'actuals' 不包括 mutate
library(dplyr)
dfone_new <- dfone %>%
group_by(subproduct) %>%
mutate(actuals = replace(actuals, actuals == 0,
mean(actuals[actuals != 0], na.rm = TRUE))) %>%
ungroup
或者在complete
中,如果我们不将其更改为0,则更直接地使用na.aggregate
从zoo
默认使用函数mean
替换 NA
个元素
library(tidyr)
library(zoo)
dfone_new <- dfone %>%
complete(date = seq.Date(from = min(date),
to = as.Date('2021-06-01'), by = 'month'),
nesting(subproduct)) %>%
group_by(subproduct) %>%
mutate(actuals = na.aggregate(actuals)) %>%
ungroup
-输出
dfone_new
# A tibble: 195 x 3
date subproduct actuals
<date> <chr> <dbl>
1 2019-01-01 1 5.67
2 2019-01-01 2 5.01
3 2019-01-01 x 5.00
4 2019-01-01 y 4.98
5 2019-02-01 1 3.97
6 2019-02-01 2 5.42
7 2019-02-01 x 5.09
8 2019-02-01 y 3.98
9 2019-03-01 1 5.18
10 2019-03-01 2 5.08
# … with 185 more rows