[英]How to convert the result of lapply with NA items to a data frame in R?
I have a list as a result of an lapply
where the function is covered with a trycatch
function because I have a super long list that I need my code to go through without being stopped by any error. 由于有一个
lapply
,因此我有一个列表,其中该函数用trycatch
函数覆盖了,因为我有一个超长列表,需要我的代码通过它而不被任何错误停止。 The result looks like this: 结果看起来像这样:
I tried using 我尝试使用
test12 <- data.frame(matrix(unlist(y), nrow=length(y), byrow = T))
However, the data frame looks very strange and it's not really in the order I wanted. 但是,数据框看起来很奇怪,而且并不是按照我想要的顺序排列。 It looks like the NA [[20]] has created a break in the data frame.
看起来NA [[20]]在数据帧中创建了一个中断。 Anyone has experience on how to convert this list into a data frame?
任何人都有关于如何将此列表转换为数据框的经验? The expected columns are:
Title
, Description
, and Keywords
, and is it possible to have those NA like item [[20]] as NA value in all 3 columns? 预期的列为:
Title
, Description
和Keywords
,是否有可能在所有3列中将那些类似于项[[20]]的NA作为NA值? Thank you. 谢谢。
With base R we can do 使用基数R,我们可以做
do.call(rbind.data.frame, y)
# Title Description Keywords
#2 Tetris Layout NA NA
#21 Tetris Layout NA NA
#3 <NA> NA NA
#4 Tetris Layout NA NA
data 数据
y <- list(list(Title = "Tetris Layout", Description = NA, Keywords= NA),
list(Title = "Tetris Layout", Description = NA, Keywords= NA),
NA,
list(Title = "Tetris Layout", Description = NA, Keywords= NA))
Another option with tidyverse
tidyverse
另一种选择
library(tidyverse)
map_df(liste, as_tibble) %>%
select(1:3)
# A tibble: 4 x 3
# Title Description Keyword
# <chr> <chr> <chr>
#1 xxx xxx2 xxx3
#2 yyy yyy2 yyy3
#3 <NA> <NA> <NA>
#4 zzz zzz2 zzz3
liste <- list(list("Title" = "xxx", "Description" = "xxx2", "Keyword" = "xxx3"),
list("Title" = "yyy", "Description" = "yyy2", "Keyword" = "yyy3"),
NA,
list("Title" = "zzz", "Description" = "zzz2", "Keyword" = "zzz3"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.