0

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_))

Nik
  • 39
  • 7
  • 1
    Welcome to StackOverlflow. Please take a look at how to provide minimun reproducible example here and make sure to add the data you have and what you expect. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – boski Apr 11 '19 at 07:43
  • In addition to the above, please post the expected output and of course your attempt and where it failed – Sotos Apr 11 '19 at 07:44
  • you could create a variable with your numbers, and use that both times you call `case_when`, i.e. `my_numbers <- c(1, 2, 4, 6)`, and then `number %in% my_numbers` as the left hand side of your `case_when`-formula. – crlwbm Apr 12 '19 at 18:57
  • The problem is that I have around 200 numbers in groups of 3-5 in another dataset. Therefore, I would stil have to write the same 30-40 vector names each time I want to add a column. – Nik Apr 24 '19 at 10:13

2 Answers2

0

You can create a data frame with your keys to replace and use the merge function :

n <- 4
df <- data.frame(number = sample(1:n, 10, replace = TRUE))
df
>   number
1       2
2       2
3       1
4       4
5       4
6       1
7       4
8       3
9       1
10      3


df_text <- data.frame(number = 1:n, text_a = paste0("text", 1:n), text_b = paste0("text", 1:n+n))
df_text
>  number text_a text_b
1      1  text1  text5
2      2  text2  text6
3      3  text3  text7
4      4  text4  text8

merge(df, df_text)
>   number text_a text_b
1       1  text1  text5
2       1  text1  text5
3       1  text1  text5
4       2  text2  text6
5       2  text2  text6
6       3  text3  text7
7       3  text3  text7
8       4  text4  text8
9       4  text4  text8
10      4  text4  text8

You can use sort = FALSE in merge if order matters

Clemsang
  • 5,053
  • 3
  • 23
  • 41
0

If you are just starting out with R, I would recommend looking into "R for data science" (https://r4ds.had.co.nz/) by Hadley Wickham. Great resource.

Here's a solution to what I think you are trying to do:

library(tidyverse)
ds <- tibble(number = 1:5)

ds %>% 
mutate(
  type_a = case_when(
    number == 1 ~ "some_text1",
    number == 2 | number == 3 ~ "some_text3",
    TRUE ~ NA_character_
  ), 
  type_b = case_when(
    number == 1 ~ "some_text2",
    number == 2 | number == 3 ~ "some_text4",
    TRUE ~ NA_character_
  )
)
crlwbm
  • 502
  • 2
  • 10
  • Thank you, this works perfectly. However, is there a way to simplify the code if you have e.g. 5 numbers that should have the same corrosponding text? Instead of writing "number == 1| number == 2 | number == 3 number == 4| number == 5 | ~ "some_text1"", it might be simplifyed with "number == 1,2,3,4,5 ~ "some_text1""? (sorry for not posting the code correctly, still looking into how to use stackoverflow, I will try and post my code when it is finalized) – Nik Apr 11 '19 at 10:41
  • Look into the `%in%` operator (i.e. the `match` function). I think this is what you are looking for: `number %in% 1:5` – crlwbm Apr 11 '19 at 16:31