TarJae
TarJae

Reputation: 78927

How to check if vector elements are in grouped column of a dataframe and add a binary column (yes, no)

I have a dataframe with one column of letters A to Z (name_x) and another column with values (value_x).

For simplicity this 26 rows represent a group of multiple rows.

I want to check if the values in the vector vocals vocals <- c("A", "E", "I", "O", "U") are in the column name_x of the dataframe df and append a third column to the dataframe with 1 if yes and 0 if no. I tried it with case_when function from dplyr and get this error:

Fehler: Problem with mutate() input vocal_yes. x Input vocal_yes can't be recycled to size 26. i Input vocal_yes is case_when(vocals %in% name_x ~ 1, TRUE ~ 0). i Input vocal_yes must be size 26 or 1, not 5. Run rlang::last_error() to see where the error occurred.

I understand the problem. Is there a way to overcome this problem. Many thanks.

The code:

library(dplyr)

# constructing the dataframe
name_x <- LETTERS[1:26]
value_x <- sample.int(100, 26)

df <- data.frame(name_x, value_x)

# vector vocals
vocals <- c("A", "E", "I", "O", "U")
# vector consonant
consonant <- c("B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z")

df1 <- df %>% 
  mutate(vocal_yes = case_when(vocals %in% name_x ~ 1,
                               TRUE ~ 0)
         )

Upvotes: 1

Views: 66

Answers (2)

akrun
akrun

Reputation: 887048

WE can also do this without case_when

library(dplyr)
df  <- df %>%
          mutate(vocal_yes = +(name_x %in% vocals))

Upvotes: 1

AnilGoyal
AnilGoyal

Reputation: 26218

df1 <- df %>% 
  mutate(vocal_yes = case_when(name_x %in% vocals ~ 1,
                               TRUE ~ 0)
         )

This will solve the problem if there is only one letter in name_x.

For better understanding try this simple code

vocals <- c('a', 'e', 'i', 'o', 'u')

letters %in% vocals

vocals %in% letters

Upvotes: 1

Related Questions