Reputation: 117
Say I have a list of every single letter in the alphabet
chars = c('a', 'b', 'c', 'd',...., 'y', 'z')
and a few lists
beg_vowels = c('a', 'e', 'i')
end_vowels = c('o', 'u', 'y')
lucky_constants = c('b', 'f', 'g')
I want to create a list the same size as the chars list but if the letter at that index in chars is in beg_vowels, its value is 'BV', if it's in end_vowels, its value is 'EV', if it's in lucky-constants its value is 'LC', and if it's not in any of them, its value is 'Unique'. So this new list, call it categor, should be something like
categor = c('BV', 'LC', 'Unique', ..., 'Unique', 'Unique')
I am planning to apply this concept for a very large list, so iterating through the chars list and comparing element by element is way too slow.
Upvotes: 1
Views: 30
Reputation: 887881
An option with tidyverse
list
(dplyr::lst
)enframe
unnest
the list
columnright_join
) with the 'chars' converted to a tibble
arrange
the rows after replacing the NA with 'Unique'pull
the column as a vector
library(dplyr)
library(stringr)
library(tibble)
library(tidyr)
lst(beg_vowels, end_vowels, lucky_constants) %>%
enframe %>%
unnest(c(value)) %>%
mutate(new = toupper(str_replace(name, "^(.).*_(.).*", "\\1\\2"))) %>%
right_join(tibble(value = chars)) %>%
mutate(new = replace_na(new, "Unique")) %>%
arrange(value) %>%
pull(new)
-output
[1] "BV" "LC" "Unique" "Unique" "BV" "LC" "LC" "Unique" "BV" "Unique" "Unique" "Unique" "Unique" "Unique" "EV" "Unique" "Unique"
[18] "Unique" "Unique" "Unique" "EV" "Unique" "Unique" "Unique" "EV" "Unique"
chars <- letters
Upvotes: 1