我正在尝试从大量列表中提取值,例如以下列表(与该列表一样具有数千个第一级条目)。这些列表的第一层结构都相同,但是要提取的信息量/形状在以下级别上有所不同。
library(tidyverse)
split_extract <- list(structure(c("1 Introduction ", "2 Intermediaries and technological innovation systems ",
"2.1 Intermediaries’ support for (eco)-innovation ", "2.2 Technological innovation systems (TIS) ",
"2.3 Linking functions thinking from TIS to intermediaries’ support roles ",
"3 The analytical approach ", "3.1 Step 1: defining the study focus ",
"3.2 Step 2: identify intermediaries in the context ", "3.3 Step 3: mapping roles of intermediaries ",
"3.4 Step 4: assessing the potential roles of intermediaries in eco-innovation ",
"3.5 Step 5: recommendations for intermediaries and their key stakeholders ",
"4 Example: analysing the potential roles of intermediaries to support eco-innovation in the region of Scania and North Rhine Westphalia ",
"4.1 Step 1 – define the study focus ", "4.2 Step 2 – identify intermediaries in the context ",
"4.3 Step 3 – map roles of the roles of intermediaries in eco-innovation ",
"4.4 Step 4 – assess the roles of intermediaries ", "5 Discussion ",
"6 Conclusions and further research ", NA, NA, ".1", ".2", ".3",
NA, ".1", ".2", ".3", ".4", ".5", NA, ".1", ".2", ".3", ".4",
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, "Introduction ", "Intermediaries and technological innovation systems ",
"Intermediaries’ support for (eco)-innovation ", "Technological innovation systems (TIS) ",
"Linking functions thinking from TIS to intermediaries’ support roles ",
"The analytical approach ", "Step 1: defining the study focus ",
"Step 2: identify intermediaries in the context ", "Step 3: mapping roles of intermediaries ",
"Step 4: assessing the potential roles of intermediaries in eco-innovation ",
"Step 5: recommendations for intermediaries and their key stakeholders ",
"Example: analysing the potential roles of intermediaries to support eco-innovation in the region of Scania and North Rhine Westphalia ",
"Step 1 – define the study focus ", "Step 2 – identify intermediaries in the context ",
"Step 3 – map roles of the roles of intermediaries in eco-innovation ",
"Step 4 – assess the roles of intermediaries ", "Discussion ",
"Conclusions and further research ", NA, NA, NA, NA, NA, NA,
"Step", "Step", "Step", "Step", "Step", NA, "Step", "Step", "Step",
"Step", NA, NA), .Dim = c(18L, 5L)))
我创建了一个简短的函数,我想将index
作为参数传递给它,可以是一个简单的整数(例如:1
),可以很容易地插入它,也可以作为格式为"i,j"
的字符串,供以后在调用中进行评估。在这种情况下,通常可能类似于",1"
。
lext <- function(list, index) {
if(typeof(index) == "character") {index <- rlang::parse_expr(index)}
map(1:length(list), ~list[[.x]][rlang::eval_bare(index)])
}
l <- lext(split_extract, index = ",1")
但是,我收到以下错误,这向我表明我采用的解析路线是错误的...但是我不知道该怎么做。
#> Error in parse(text = x): <text>:1:1: unexpected ','
#> 1: ,
#> ^
任何帮助将不胜感激!
答案 0 :(得分:2)
将...
与lapply
一起使用,但不能与purrr
...
lext <- function(list, ...) {
lapply(seq_along(list), function(x) list[[x]][...])
}
lext(split_extracted, , 1)
或者简单地
lext <- function(list, ...) {
lapply(list, function(x) x[...])
}
答案 1 :(得分:1)
怎么样?
lext <- function(list, index) {
if(typeof(index) == "character") {index <- rlang::parse_expr(sprintf(".x[%s]", index))}
map(list, ~rlang::eval_bare(index))
}
l <- lext(split_extract, index = ",1")
[[1]]
[1] "1 Introduction "
[2] "2 Intermediaries and technological innovation systems "
[3] "2.1 Intermediaries’ support for (eco)-innovation "
[4] "2.2 Technological innovation systems (TIS) "
[5] "2.3 Linking functions thinking from TIS to intermediaries’ support roles "
[6] "3 The analytical approach "
[7] "3.1 Step 1: defining the study focus "
[8] "3.2 Step 2: identify intermediaries in the context "
[9] "3.3 Step 3: mapping roles of intermediaries "
[10] "3.4 Step 4: assessing the potential roles of intermediaries in eco-innovation "
[11] "3.5 Step 5: recommendations for intermediaries and their key stakeholders "
[12] "4 Example: analysing the potential roles of intermediaries to support eco-innovation in the region of Scania and North Rhine Westphalia "
[13] "4.1 Step 1 – define the study focus "
[14] "4.2 Step 2 – identify intermediaries in the context "
[15] "4.3 Step 3 – map roles of the roles of intermediaries in eco-innovation "
[16] "4.4 Step 4 – assess the roles of intermediaries "
[17] "5 Discussion "
[18] "6 Conclusions and further research "
另一项测试:
l <- lext(split_extract, index = "2,1")
[[1]]
[1] "2 Intermediaries and technological innovation systems "