【发布时间】:2021-08-31 11:20:33
【问题描述】:
我有一个这样的数据框,
Industry = c("Industry1", "Industry1", "Industry1", "Industry2", "Industry2", "Industry3")
calc = c(-1.2345, 67890, 45678, -9.86544, 32456, 56789)
cy = c(0, 2019, 2017, 2016, 0, 2015)
py = c(0, 2018, 2016, 2015, 0, 2014)
data = data.frame(Industry, calc, cy, py, stringsAsFactors = FALSE)
当 cy = 0 和 py = 0 时,我想通过根据行业分组计算 calc 列的平均值来替换 calc 列中的负值。我尝试了以下方法,
data = data %>%
group_by("Industry") %>%
mutate(calc = ifelse(cy == 0 & py == 0, summarise(calc = mean(calc)), calc))
但这不起作用。谁能帮我解决这个问题?
【问题讨论】:
-
您的
data显示此错误Error in data.frame(Industry, calc, cy, py, stringsAsFactors = FALSE) : arguments imply differing number of rows: 6, 7 -
我已经改正了!
标签: r if-statement dplyr conditional-statements