【发布时间】:2021-09-22 09:28:01
【问题描述】:
我需要一些帮助,用周围行中已经存在的其他值填充具有“NA”值的单元格。
我目前有一个投资者及其活动的面板数据集。有些行丢失了,所以我完成了面板以包含这些行,将金融交易信息替换为“0”值。
其他变量与更广泛的公司特征相关,例如区域和战略。我不确定如何为每个公司复制这些。
这是我目前的代码。
df <- df %>%
group_by(investor) %>%
mutate(min = min(dealyear, na.rm = TRUE),
max = max(dealyear, na.rm = TRUE)) %>%
complete(investor, dealyear = min:max, fill = list(counttotal=0, countgreen=0, countbrown=0)) %>%
完成前的数据示例 - 缺少 2004 年通知。
| investor | dealyear | dealcounts | strategy | region |
|---|---|---|---|---|
| 123IM | 2002 | 5 | buyout | europe |
| 123IM | 2003 | 5 | buyout | europe |
| 123IM | 2005 | 5 | buyout | europe |
| 123IM | 2006 | 5 | buyout | europe |
完成后的数据示例,其中添加了缺失的行
| investor | dealyear | dealcounts | strategy | region |
|---|---|---|---|---|
| 123IM | 2002 | 5 | buyout | europe |
| 123IM | 2003 | 5 | buyout | europe |
| 123IM | 2004 | 0 | NA | NA |
| 123IM | 2005 | 5 | buyout | europe |
| 123IM | 2006 | 5 | buyout | europe |
我将如何用每个投资公司的相应信息替换这些 NA 值?
非常感谢
罗里
【问题讨论】:
标签: r tidyverse dplyr data-wrangling