简体   繁体   English

将indeces作为R中的参数传递

[英]Passing indeces as arguments in R

I am trying to extract values from a large number of lists such as the one below (which has thousands of first-level entries just like that one). 我试图从大量列表中提取值,例如下面的列表(其中包含数千个第一级条目)。 The lists are all the same in their first-level structure, but the amount/shape of information to be extracted varies for the level below. 列表在它们的第一级结构中都是相同的,但是要提取的信息的数量/形状因下面的级别而异。

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)))

I have created a short function to which I'd like to pass index as an argument, either as a simple integer (eg: 1 ), which can easily be plugged in, or as a string in the form "i,j" for later evaluation inside the call. 我创建了一个简短函数,我想将index作为参数传递,或者作为一个简单的整数(例如: 1 ),可以很容易地插入,或者作为"i,j"形式的字符串后来评估里面的电话。 Often this may be something like ",1" as in this case. 通常这可能类似于",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")

However, I get the error below, which suggests to me that the parsing route I've taken is wrong... but I don't know how else to do this. 但是,我得到下面的错误,这告诉我,我采取的解析路线是错误的...但我不知道如何做到这一点。

#> Error in parse(text = x): <text>:1:1: unexpected ','
#> 1: ,
#>     ^

Any help would be much appreciated! 任何帮助将非常感激!

Using ... works with lapply , but not with purrr ... 使用...适用于lapply ,但不适用于purrr ...

lext <- function(list, ...) {
  lapply(seq_along(list), function(x) list[[x]][...])
}
lext(split_extracted, , 1)

Or simply 或者干脆

lext <- function(list, ...) {
  lapply(list, function(x) x[...])
}

How about this? 这个怎么样?

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 "  

Another test: 另一个测试:

l <- lext(split_extract, index = "2,1")

[[1]]
[1] "2 Intermediaries and technological innovation systems "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM