【问题标题】:R Filling missing values with NA for a data frameR用NA填充数据框的缺失值
【发布时间】:2018-06-26 14:02:09
【问题描述】:

我目前正在尝试使用以下列表创建数据框

location <- list("USA","Singapore","UK")
organization <- list("Microsoft","University of London","Boeing","Apple")
person <- list()
date <- list("1989","2001","2018")
Jobs <- list("CEO","Chairman","VP of sales","General Manager","Director")

当我尝试创建一个数据框时,我得到了(明显的)错误,即列表的长度不相等。我想找到一种方法使列表长度相同,或者用“NA”填充丢失的数据框条目。经过一番搜索,我一直无法找到解决方案

【问题讨论】:

  • 你打算如何安排这些排队?什么日期对应什么工作等?
  • 假设您希望 NA 在最后,我会使用与 this 类似的解决方案,并具有固定的 n
  • @camille 为了这个例子,排队无关紧要

标签: r list dataframe na


【解决方案1】:

这里是purrrtidyverse 的一部分)和基本 R 解决方案,假设您只想用NA 填充每个列表中的剩余值。我将任何列表的最大长度设为len,然后对于每个列表执行rep(NA),以获得 that 列表的长度与 any 的最大长度之间的差异em>列表。

library(tidyverse)

location <- list("USA","Singapore","UK")
organization <- list("Microsoft","University of London","Boeing","Apple")
person <- list()
date <- list("1989","2001","2018")
Jobs <- list("CEO","Chairman","VP of sales","General Manager","Director")

all_lists <- list(location, organization, person, date, Jobs)
len <- max(lengths(all_lists))

使用purrr::map_dfc,您可以映射列表列表,根据需要添加NAs,转换为字符向量,然后在一次管道调用中获取所有这些向量cbinded 的数据框:

map_dfc(all_lists, function(l) {
  c(l, rep(NA, len - length(l))) %>%
    as.character()
})
#> # A tibble: 5 x 5
#>   V1        V2                   V3    V4    V5             
#>   <chr>     <chr>                <chr> <chr> <chr>          
#> 1 USA       Microsoft            NA    1989  CEO            
#> 2 Singapore University of London NA    2001  Chairman       
#> 3 UK        Boeing               NA    2018  VP of sales    
#> 4 NA        Apple                NA    NA    General Manager
#> 5 NA        NA                   NA    NA    Director

在基础 R 中,您可以在列表列表中使用 lapply 相同的函数,然后使用 Reducecbind 生成的列表并将其转换为数据框。采取两步而不是 purrr 的一步:

cols <- lapply(all_lists, function(l) c(l, rep(NA, len - length(l))))
as.data.frame(Reduce(cbind, cols, init = NULL))
#>          V1                   V2 V3   V4              V5
#> 1       USA            Microsoft NA 1989             CEO
#> 2 Singapore University of London NA 2001        Chairman
#> 3        UK               Boeing NA 2018     VP of sales
#> 4        NA                Apple NA   NA General Manager
#> 5        NA                   NA NA   NA        Director

对于这两者,您现在可以随意设置名称。

【讨论】:

    【解决方案2】:

    你可以这样做:

    data.frame(sapply(dyem_list, "length<-", max(lengths(dyem_list))))
    
       location         organization person date            Jobs
    1       USA            Microsoft   NULL 1989             CEO
    2 Singapore University of London   NULL 2001        Chairman
    3        UK               Boeing   NULL 2018     VP of sales
    4      NULL                Apple   NULL NULL General Manager
    5      NULL                 NULL   NULL NULL        Director
    

    其中dyem_list 如下:

    dyem_list <- list(
      location = list("USA","Singapore","UK"),
      organization = list("Microsoft","University of London","Boeing","Apple"),
      person = list(),
      date = list("1989","2001","2018"),
      Jobs = list("CEO","Chairman","VP of sales","General Manager","Director")
    )
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多