Home > Software engineering >  Subset between elements in a list using names
Subset between elements in a list using names

Time:03-24

I have a vector in R and would like to select all elements in the vector between two positions.

I know I can achieve this through by stating the location of the elements within the list using the example below.

test <- c('Group1', 'Test', 'sample', 'abcd', '1234', 'ghsnd', 'asdcd')
test[c(2:6)]

I would instead like to do this by calling the name of the elements in the list. For example calling 'Test':'ghsnd'.

CodePudding user response:

I believe you are lokking for something like this (not sure if it it very efficient)

test <- c('Group1', 'Test', 'sample', 'abcd', '1234', 'ghsnd', 'asdcd')

start <- "Test"
end   <- "ghsnd"
temp_str <- "@@@"

regex.pattern = paste(".*(", start, ".*", end, ").*", sep = temp_str)  

ans <- unlist(strsplit(gsub(regex.pattern, "\\1", paste0(test, collapse = temp_str)), temp_str))
ans[ans != ""]

[1] "Test"   "sample" "abcd"   "1234"   "ghsnd" 

CodePudding user response:

if I understood correctly

test <- c('Group1', 'Test', 'sample', 'abcd', '1234', 'ghsnd', 'asdcd')
test <- setNames(object = test, nm = test)
test["ghsnd"]
#>   ghsnd 
#> "ghsnd"

Created on 2022-03-23 by the reprex package (v2.0.1)

CodePudding user response:

A different approach, using the indexes... might not work with duplicate verctor entries

test[sum(1:length(test) * (test == "Test")):sum(1:length(test) * (test == "ghsnd"))]

CodePudding user response:

Based on Wimpel's answer I was able to write a function that does the job.

extract_between <- function(first_str, last_str, vector_list){

first_el <- paste0(all_of(first_str)) 
last_el  <- paste0(all_of(last_str))

vector_list[sum(1:length(vector_list) * (vector_list == first_el)):sum(1:length(vector_list) * (vector_list == last_el))] }

extract_between('Test', 'ghsnd', test)
  •  Tags:  
  • r
  • Related