簡體   English   中英

將indeces作為R中的參數傳遞

[英]Passing indeces as arguments in R

我試圖從大量列表中提取值,例如下面的列表(其中包含數千個第一級條目)。 列表在它們的第一級結構中都是相同的,但是要提取的信息的數量/形狀因下面的級別而異。

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: ,
#>     ^

任何幫助將非常感激!

使用...適用於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[...])
}

這個怎么樣?

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 "

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM