Pass a string variable in a loop within in a pipe in R for the mutate function

94 views Asked by At

I am not able to get the x variables in the mutate function in R below to become floating variables. Can someone please help me trouble shoot this code.

library(dplyr)
List<-c("Go","Rust")

env <- environment()

for (i in c(1:length(List))) {

   x=List[i]

   "x" %>% assign(List[i], envir = env)
   print(x)
   subData<-subData %>% 
      mutate(x = case_when( str_detect(ColumnName, "x") ~ 1, TRUE ~ 0))
}

Rust and Go are words that are being found in a column of strings and then a new column name is being created with that word. That is what the mutate function is doing.

These two lines of code work when I run them, but when I try and loop through the list they don't work.

 subData <- subData %>% 
 mutate(Rust = case_when(str_detect(LanguageWantToWorkWith, "Rust") ~ 1, 
  TRUE ~ 0))

subData <- subData %>% 
mutate(Go = case_when(str_detect(LanguageWantToWorkWith, "Go") ~ 1, TRUE 
~ 0))

Output

Thank you, Kelly Fitzpatrick

I can't get the x variable in the mutate function to loop through the list of words.

1

There are 1 answers

2
AndS. On

It seems to me that you just want to one-hot-encode a variable from a string of variables. Here is what I would do:

library(tidyverse)

subData <- tibble(ColumnName = c("R;Python;Rust", "Go;R"))

List<-c("Go","Rust")

add_binary_col <- function(data, checks){
  data |>
    mutate(lang = str_split(ColumnName, ";"),
           cols = map(lang, ~checks[checks %in% .x]),
           v = 1) |>
    unnest(cols, keep_empty = TRUE) |>
    pivot_wider(names_from = cols, values_from = v, values_fill = 0) |>
    select(-lang, -starts_with("NA"))
}

add_binary_col(subData, List)
#> # A tibble: 2 x 3
#>   ColumnName     Rust    Go
#>   <chr>         <dbl> <dbl>
#> 1 R;Python;Rust     1     0
#> 2 Go;R              0     1
add_binary_col(subData, c("R", "Python", "Rust"))
#> # A tibble: 2 x 4
#>   ColumnName        R Python  Rust
#>   <chr>         <dbl>  <dbl> <dbl>
#> 1 R;Python;Rust     1      1     1
#> 2 Go;R              1      0     0
add_binary_col(subData, c("Python", "Rust"))
#> # A tibble: 2 x 3
#>   ColumnName    Python  Rust
#>   <chr>          <dbl> <dbl>
#> 1 R;Python;Rust      1     1
#> 2 Go;R               0     0