【问题标题】:Replacing NA values with values from neighbouring rows [duplicate]用相邻行的值替换 NA 值
【发布时间】: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


    【解决方案1】:

    您可以使用completegroup_by 作为-

    library(dplyr)
    library(tidyr)
    
    df %>%
      group_by(investor) %>%
      complete(dealyear = min(dealyear):max(dealyear), 
               fill = list(dealcounts = 0)) %>%
      ungroup
    
    #  investor dealyear dealcounts strategy region
    #  <chr>       <int>      <dbl> <chr>    <chr> 
    #1 123IM        2002          5 buyout   europe
    #2 123IM        2003          5 buyout   europe
    #3 123IM        2004          0 NA       NA    
    #4 123IM        2005          5 buyout   europe
    #5 123IM        2006          5 buyout   europe
    

    如果您想替换strategyregion 列中的NA,您可以使用fill

    df %>%
      group_by(investor) %>%
      complete(dealyear = min(dealyear):max(dealyear), 
               fill = list(dealcounts = 0)) %>%
      fill(strategy, region) %>%
      ungroup
    
    #  investor dealyear dealcounts strategy region
    #  <chr>       <int>      <dbl> <chr>    <chr> 
    #1 123IM        2002          5 buyout   europe
    #2 123IM        2003          5 buyout   europe
    #3 123IM        2004          0 buyout   europe
    #4 123IM        2005          5 buyout   europe
    #5 123IM        2006          5 buyout   europe
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 2012-11-10
      • 1970-01-01
      • 2017-04-22
      • 2010-11-18
      • 2013-03-15
      相关资源
      最近更新 更多