Crazed
Crazed

Reputation: 117

Forming a column in R with conditionals

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

Answers (1)

akrun
akrun

Reputation: 887881

An option with tidyverse

  1. Get the objects in a named list (dplyr::lst)
  2. Convert the named list to a tibble - enframe
  3. unnest the list column
  4. Extract the substring from the 'name', convert it to upper case
  5. Do a join (right_join) with the 'chars' converted to a tibble
  6. arrange the rows after replacing the NA with 'Unique'
  7. 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"

data

chars <- letters

Upvotes: 1

Related Questions