[英]R Filling missing values with NA for a data frame
我目前正在嘗試使用以下列表創建數據框
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”填充丟失的數據框條目。 經過一些搜索后,我無法找到解決方案
這里是purrr
(部分tidyverse
)和基礎R解決方案,假設你只是想在每個列表,以填補剩余值NA
。 我采取任何列表作為的最大長度len
,然后為每個表做rep(NA)
該列表的長度和任何列表的最大長度之間的差異。
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
,您可以映射列表列表,根據需要添加NA
,轉換為字符向量,然后獲得所有這些向量在一個管道調用中cbind
的數據幀:
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
跨列表列表相同的功能,然后使用Reduce
到cbind
所產生的名單,並將其轉換成數據幀。 采取兩個步驟而不是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
對於這兩者,您現在都可以根據需要設置名稱。
您可以這樣做:
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")
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.