Considering the following dataset:
df <- data.frame (Treatment = c("Low", "Low", "Low", "Med", "Med", "High", "High", "Med", "Low", "Med", "High", "High"),
Value = c( 23,4,56,76,89,86,66,31,48,51,75,101))
Treatment | Value |
---|---|
Low | 23 |
Low | 4 |
Low | 56 |
Med | 76 |
Med | 89 |
High | 86 |
High | 66 |
Med | 31 |
Low | 48 |
Med | 51 |
High | 75 |
High | 101 |
and I would like to create a column so the data frame looks like:
Treatment | Value | I.D |
---|---|---|
Low | 23 | Low1 |
Low | 4 | Low2 |
Low | 56 | Low3 |
Med | 76 | Med1 |
Med | 89 | Med2 |
High | 86 | High1 |
High | 66 | High2 |
Med | 31 | Med3 |
Low | 48 | Low4 |
Med | 51 | Med4 |
High | 75 | High3 |
High | 101 | High4 |
Basically I'd like to create a column with individual ID for each row based on the level they belong to.
I have tried this:
df %>% group_by (Treatment) %>%
mutate (I.D = case_when(
Treatment == "Low" ~ paste ("Low",seq(1,4)),
Treatment == "Med" ~ paste ("Med",seq(1,4)),
Treatment == "High" ~ paste("High",seq(1,4))))
Thanks very much in advance
CodePudding user response:
You can use row_number()
:
library(dplyr)
df |>
group_by(Treatment) |>
mutate(id = paste0(Treatment, row_number()))
Output:
#> # A tibble: 12 x 3
#> # Groups: Treatment [3]
#> Treatment Value id
#> <chr> <dbl> <chr>
#> 1 Low 23 Low1
#> 2 Low 4 Low2
#> 3 Low 56 Low3
#> 4 Med 76 Med1
#> 5 Med 89 Med2
#> 6 High 86 High1
#> 7 High 66 High2
#> 8 Med 31 Med3
#> 9 Low 48 Low4
#> 10 Med 51 Med4
#> 11 High 75 High3
#> 12 High 101 High4
Created on 2022-06-14 by the reprex package (v2.0.1)
CodePudding user response:
If you don't want the whitespace after "Low" just use paste0
instead of 'paste'