In RStudio, I want to add two new columns to a dataframe. These two columns should be filled with text based on conditional statements from an already existing column in the dataframe.
Data.frame
Variable is: number c(1,2,3,4, etc.)
I need a code (statement) creating and filling out the two new columns "type_a" and "type_b" based on the number in the "number" column. I have to use the exact number, so "if 1 in "number" fill out "type_a" with "some_text1" and fill out "type_b" with "some_text2". Next line will be "if 2 or 3 in "number" fill out "type_a" with "some_text3" and "type_b" with "some_text4".
This should be fairly simple, however, I am new to R and currently following courses.
Thanks,
EDIT:
Thanks to the answers below, I have now managed to do what I want with this code. The question now is, can I somehow include the "type_b" statement in the first "case_when" so I dont have to write the list of numbers twice, or is it only possible to include one column per "case_when"? (in SAS I would create the two columns type_a and type_b first and then write "if number in (1,2,4,6) then do; type_a='some_text'; type_b ='some_text2; end;).
Thanks
ds <- tibble(number = 1:6)
ds %>%
mutate(
type_a = case_when(
number %in% c(1,2,4,6) ~ "some_text", TRUE ~ NA_character_
),
type_b = case_when(
number %in% c(1,2,4,6) ~ "some_text2", TRUE ~NA_character_
)
)
#The code below do not work, but hopefully you get the idea!
library(tidyverse)
ds <- tibble(number = 1:6)
ds %>%
mutate(
type_a, type_b = case_when(
number %in% c(1,2,4,6) ~ "some_text", ~ "some_text2",
TRUE ~ NA_character_))