我正在尝试处理许多列表,但我丢失了names
某些列表元素。
该列表如下所示:
> myLists2
[[1]]
NULL
[[2]]
[[2]][[1]]
title company date_range location
"Founder | Co-CEO" "someCompany" "ene. de 2018 \023 actualidad" "Europe"
description li_company_url
"some description 1" "https://www.google.com"
[[2]][[2]]
title company date_range location
"Another title" "someCompany2" "ene. de 2019 \023 actualidad" "USA"
description li_company_url
"Another Description" "https://www.yahoo.com"
[[2]][[3]]
title company date_range location
"Another title 3" "Another company 3" "sept. de 2018 \023 actualidad" "Europe"
description li_company_url
"Another description 3" "https://www.stackexchange.com"
如果在哪里跑步,names(myLists2[[2]][[1]])
我会得到以下信息:
[1] "title" "company" "date_range" "location" "description" "li_company_url"
在不同的列表中,名称的数量可能会略有不同,我想创建一个新列,该列出names
现在中data.frame
。
运行:
hh <- myLists2[[2]] %>% data.frame() %>% rownames_to_column("tag")
给了我一个不错的数据框,在这里我使用该rownames_to_column()
函数保存行名,但是当列表元素的长度不同时,这给我一个错误。
错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,:参数表示行数不同:5,6
我发现对此的解决方案是使用bind_rows()
。运行:
myLists2[[2]] %>% bind_rows()
给我一个,tibble
但我names
从列表中丢失了。运行:
myLists2[[2]] %>% bind_rows(.id = "myID")
似乎也不能解决问题,因为它给了我一个新的列,从1到3。
我的问题是,我该如何使用bind_rows()
(对不同的列长不敏感)并将其names
从列表另存为列?
数据:
myLists2 <- list(NULL, list(c(title = "Founder | Co-CEO", company = "someCompany",
date_range = "ene. de 2018 \023 actualidad", location = "Europe",
description = "some description 1", li_company_url = "https://www.google.com"
), c(title = "Another title", company = "someCompany2", date_range = "ene. de 2019 \023 actualidad",
location = "USA", description = "Another Description", li_company_url = "https://www.yahoo.com"
), c(title = "Another title 3", company = "Another company 3",
date_range = "sept. de 2018 \023 actualidad", location = "Europe",
description = "Another description 3", li_company_url = "https://www.stackexchange.com"
)))
编辑:(添加新列表)
myNewList <- list(list(c(title = "Founder | Co-CEO", company = "some company",
date_range = "ene. de 2018 \023 actualidad", location = "Europe",
description = "some description",
li_company_url = "https://www.google.com"
), c(title = "some thing here", company = "some company",
date_range = "ene. de 2019 \023 actualidad", location = "USA",
description = "another description",
li_company_url = "https://www.yahoo.com")
), list(c(title = "CEO", company = "another company",
date_range = "2012 \023 actualidad", description = "some other description",
li_company_url = ""), c(title = "job title",
company = "company name", date_range = "ene. de 2005 \023 actualidad",
location = "Europe", description = "company description",
li_company_url = "https://www.yahoo.com"),
c(title = "job title 2", company = "company name", date_range = "1995 \023 actualidad",
description = "description",
li_company_url = ""), c(title = "job title",
company = "company name", date_range = "1992 \023 1995",
location = "USA", description = "soem company description",
li_company_url = ""), c(title = "company title", company = "company name",
date_range = "1990 \023 1992", description = "Another description",
li_company_url = "")), NULL)
这些显示了我遇到的问题:
map(myNewList, ~data.frame(.x))
map(myNewList[1], ~data.frame(.x)) # runs okay and I keep the names
map(myNewList[2], ~data.frame(.x)) # errors
map(myNewList, ~bind_rows(.x)) # runs okay but I lsoe the names
我们可以使用map_if
与data.table::transpose
后做的bind_rows
library(purrr)
library(dplyr)
library(tibble)
library(data.table)
map_if(myNewList, .p = ~ length(.) > 0,
.f = ~bind_rows(.x) %>%
data.table::transpose(., keep.names = 'title') %>%
column_to_rownames('title'),
.else = ~ NA_character_)
-输出
#[[1]]
# V1 V2
#title Founder | Co-CEO some thing here
#company some company some company
#date_range ene. de 2018 \023 actualidad ene. de 2019 \023 actualidad
#location Europe USA
#description some description another description
#li_company_url https://www.google.com https://www.yahoo.com
#[[2]]
# V1 V2 V3 V4
#title CEO job title job title 2 job title
#company another company company name company name company name
#date_range 2012 \023 actualidad ene. de 2005 \023 actualidad 1995 \023 actualidad 1992 \023 1995
#description some other description company description description soem company description
#li_company_url https://www.yahoo.com
#location <NA> Europe <NA> USA
# V5
#title company title
#company company name
#date_range 1990 \023 1992
#description Another description
#li_company_url
#location <NA>
#[[3]]
#[1] NA
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句