R: find vector in list of vectors
If you just want to determine if the vector is in the list, try
Position(function(x) identical(x, state), final_states, nomatch = 0) > 0
# [1] TRUE
Position()
basically works like match()
, but on a list. If you set nomatch = 0
and check for Position > 0
, you'll get a logical result telling you whether state
is in final_states
R: Is a vector in a list of vectors
You can use
l = list(c(2,1), c(3,2), c(2,3))
x = c(3,2)
any(sapply(l, identical, x))
In R, find elements of a vector in a list using vectorization
we can do this, seems to be the fastest by far.
v1 <- c(1, 200, 4000)
L1 <- list(1:4, 1:4*100, 1:4*1000)
sequence(lengths(L1))[match(v1, unlist(L1))]
# [1] 1 2 4
sequence(lengths(L1))[which(unlist(L1) %in% v1)]
# [1] 1 2 4
library(microbenchmark)
library(tidyverse)
microbenchmark(
akrun_sapply = {sapply(L1, function(x) which(x %in% v1))},
akrun_Vectorize = {Vectorize(function(x) which(x %in% v1))(L1)},
akrun_mapply = {mapply(function(x, y) which(x %in% y), L1, v1)},
akrun_mapply_match = {mapply(match, v1, L1)},
akrun_map2 = {purrr::map2_int(L1, v1, ~ .x %in% .y %>% which)},
CPak = {setNames(rep(1:length(L1), times=lengths(L1)), unlist(L1))[as.character(v1)]},
zacdav = {sequence(lengths(L1))[match(v1, unlist(L1))]},
zacdav_which = {sequence(lengths(L1))[which(unlist(L1) %in% v1)]},
times = 10000
)
Unit: microseconds
expr min lq mean median uq max neval
akrun_sapply 18.187 22.7555 27.17026 24.6140 27.8845 2428.194 10000
akrun_Vectorize 60.119 76.1510 88.82623 83.4445 89.9680 2717.420 10000
akrun_mapply 19.006 24.2100 29.78381 26.2120 29.9255 2911.252 10000
akrun_mapply_match 14.136 18.4380 35.45528 20.0275 23.6560 127960.324 10000
akrun_map2 217.209 264.7350 303.64609 277.5545 298.0455 9204.243 10000
CPak 15.741 19.7525 27.31918 24.7150 29.0340 235.245 10000
zacdav 6.649 9.3210 11.30229 10.4240 11.5540 2399.686 10000
zacdav_which 7.364 10.2395 12.22632 11.2985 12.4515 2492.789 10000
list of vectors in R - extract an element of the vectors
lapply(data, function(x) substr(x[startsWith(x, "no")], 4, 1000))
[[1]]
[1] "more"
[[2]]
[1] "comfort" "one"
Find Intersect of a Vector and List of Vectors
We could do
lapply(myList, intersect, y = myVector)
#$Fruits
#[1] "Banana" "Apple"
#
#$Veggies
#[1] "Spinach" "Lettuce"
#
#$Other
#[1] "Soda" "Candy"
Explanation: elements of myList
are passed to x
one by one; myVector
is passed to y
.
## check arguments of `intersect()`
args(intersect)
#function (x, y)
r - check is all elements of a list match a vector exactly
We can use lapply
:
lapply(list_1, function(x) if(all(x %in% vec_1)) TRUE else setdiff(x, vec_1))
#$x
#[1] TRUE
#$y
#[1] "foo" "bars"
Find all intersecting vectors in a list of vectors in R
Given that the list elements should be partitioned according to:
- List elements with empty intersections w.r.t. all the other list components,
- List elements with a non-empty intersection w.r.t. some other list component,
a way to achieve this in base R is as follows:
## find set components w/ empty intersections w/ all other components
isUnique <- sapply(seq_along(sets), function(i) length(intersect(sets[[i]], unlist(sets[-i]))) < 1)
## empty intersect components
sets[isUnique]
#> $e
#> [1] "e45" "e55" "e65"
#>
#> $j
#> [1] "j1" "j2" "j3"
## non-empty intersect components
sets[!isUnique]
#> $b
#> [1] "b4" "b5" "b6"
#>
#> $c
#> [1] "c2" "c3" "b4" "b5" "c6"
#>
#> $d
#> [1] "d1" "d2"
#>
#> $f
#> [1] "f4" "f5" "d1" "f6"
#>
#> $g
#> [1] "g1" "g2"
#>
#> $h
#> [1] "h5" "h6" "h7"
#>
#> $i
#> [1] "i9" "h5" "g1" "h6" "i8" "i7"
Find vector of strings in list (R)
%in%
checks presence of elements of the LHS among elements of the RHS. To treat c("a", "b")
as a single element of the RHS, it needs to be in a list:
which(l %in% list(c("a", "b")))
Other possibilities are to go element-by-element through l
with sapply
, such as
which(sapply(l,function(x) all(c("a","b") %in% x)))
# order doesn't matter, other elements allowed
which(sapply(l, identical, c("a", "b"))) # exact match, in order
R - How do I check if an element is in a list of vectors?
The most straightforward option is that you could store unique entries in another vector as you're looping through your input data.
Here's a solution without considering the positions (1 or 2) of the alphabets in your output list or the order of your input list.
dat <- list(c('a','d'),c('a','c'),c('d','e'),c('e','f'),c('b','c'),
c('f','c'),c('c','e'),c('f','b'),c('b','a'))
Dat <- list()
idx <- list()
for(i in dat){
if(!all(i %in% idx)){
Dat <- append(Dat, list(i))
## append to idx if not previously observed
if(! i[1] %in% idx) idx <- append(idx, i[1])
if(! i[2] %in% idx) idx <- append(idx, i[2])
}
}
print(Dat)
#> [[1]]
#> [1] "a" "d"
#>
#> [[2]]
#> [1] "a" "c"
#>
#> [[3]]
#> [1] "d" "e"
#>
#> [[4]]
#> [1] "e" "f"
#>
#> [[5]]
#> [1] "b" "c"
On another note, I'd advise against using T
as your vector name as it's used as TRUE
in R.
R: Find unique vectors in list of vectors
We can sort
the list
elements, apply duplicated
to get a logical index of unique elements and subset the list
based on that
list_of_vectors[!duplicated(lapply(list_of_vectors, sort))]
#[[1]]
#[1] "a" "b" "c"
#[[2]]
#[1] "b" "b" "c"
#[[3]]
#[1] "c" "c" "b"
#[[4]]
#[1] "b" "b" "c" "d"
#[[5]]
#NULL
Related Topics
Remove Unused Factor Levels from a Ggplot Bar Plot
Dplyr::Do() Requires Named Function
Got Message Unable to Load Shared Object Stats.So When R Starts
R Optimization with Equality and Inequality Constraints
Merge Overlapping Ranges into Unique Groups, in Dataframe
Consolidating Data Frames in R
Subset Data.Table by Logical Column
Can't Run Rcpp Function in Foreach - "Null Value Passed as Symbol Address"
Add Row in Each Group Using Dplyr and Add_Row()
How to Turn Gpclibpermit() to True
Removing Unused Factors from a Facet in Ggplot2
Continuous Colour of Geom_Line According to Y Value
Source Script to Separate Environment in R, Not the Global Environment