# A tibble: 6 × 7
ID alcohol germ_aversion time1_determina…¹ time2…² region decis…³
<dbl> <chr> <dbl> <dbl> <dbl> <chr> <dbl>
1 1 Group 2: non-drinkers 16 5 10 Group… 16
2 2 Group 1: drinkers 22 15 19 Group… 22
3 3 Group 1: drinkers 20 23 27 Group… 20
4 4 Group 2: non-drinkers 11 16 18 Group… 11
5 5 Group 2: non-drinkers 22 21 26 Group… 22
6 6 Group 1: drinkers 14 8 6 Group… 14
I want to separate drinkers and non-drinkers in the alcohol column
I have tried using separate(df, alcohol, into = c("drinkers", and "non-drinkers"), sep = " ") but got an eeror.
How do i go about it please ?
After the manipulation, the alcohol column should be drinkers and non drinkers only
Could you please add an example of how the data frame should look like after the desired manipulation?
Flm
November 5, 2022, 1:36pm
3
Is this what you want?
df <- tibble(alcohol = c("Group 2: non-drinkers","Group 1: drinkers","Group 1: drinkers", "Group 2: non-drinkers", "Group 2: non-drinkers","Group 1: drinkers"))
df %>%
mutate(alcohol2 = case_when(alcohol == "Group 1: drinkers" ~ "drinkers",
alcohol == "Group 2: non-drinkers" ~ "non-drinkers"))
Produces:
A tibble: 6 × 2
alcohol alcohol2
<chr> <chr>
1 Group 2: non-drinkers non-drinkers
2 Group 1: drinkers drinkers
3 Group 1: drinkers drinkers
4 Group 2: non-drinkers non-drinkers
5 Group 2: non-drinkers non-drinkers
6 Group 1: drinkers drinkers
2 Likes
Ah yes Thank you, Thank you!
Alcohol2 column has just drinkers and non-drinkers string values that corresponds with initial alcohol column of group 1 and group 2
Here are two additional approaches:
Data <- data.frame(
id = 1:10,
alcohol = sample(c('Group 1: drinkers','Group 2: non-drinkers'),10,TRUE)
)
# using tidyr::separate() as you already tried
library('tidyr'); library('dplyr')
Data |>
separate(col = alcohol, sep = "Group [1-2]{1}:", into = c('col1','alcohol'), remove = TRUE) |>
select(-col1)
#> id alcohol
#> 1 1 drinkers
#> 2 2 drinkers
#> 3 3 drinkers
#> 4 4 non-drinkers
#> 5 5 drinkers
#> 6 6 non-drinkers
#> 7 7 drinkers
#> 8 8 drinkers
#> 9 9 non-drinkers
#> 10 10 drinkers
# using stringr::str_remove()
library('stringr')
Data |>
mutate(
alcohol = str_remove(alcohol,'^Group [12]: ')
)
#> id alcohol
#> 1 1 drinkers
#> 2 2 drinkers
#> 3 3 drinkers
#> 4 4 non-drinkers
#> 5 5 drinkers
#> 6 6 non-drinkers
#> 7 7 drinkers
#> 8 8 drinkers
#> 9 9 non-drinkers
#> 10 10 drinkers
Created on 2022-11-05 with reprex v2.0.2
Kind regards
Thank you. Both approaches worked just fine!
If any of the comments was sufficient to solve your problem, you can accept one of those answers
I think you misunderstood me. There is a little check mark under every answer. Choose the answer which solved your problem and press the checkmark below that. This will indicate your problem has been solved.
system
Closed
November 14, 2022, 3:20am
11
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.