Home > Software design >  Repeat values down a grouping column using R
Repeat values down a grouping column using R

Time:12-13

I have the following data.frame:

df <- data.frame(value = 1:23)

df |> 
  mutate(group = case_when(value < 4 ~ "A",
                           value >= 4 &  value < 18 ~ "B",
                           value >= 18 & value <= 23 ~ "C"))

I want to add two more columns using mutate. The first that repeats the pattern c("white", "black") n times, for each group

Desired outcome:

   value group  color
1      1     A  white
2      2     A  black
3      3     A  white
4      4     B  white
5      5     B  black
6      6     B  white
7      7     B  ...
8      8     B
9      9     B
10    10     B
11    11     B
12    12     B
13    13     B
14    14     B
15    15     B
16    16     B
17    17     B
18    18     C  white
19    19     C  black
20    20     C  white
21    21     C  ...
22    22     C
23    23     C

The second column repeats the pattern 1:nrows twice for each group.

   value group  color distance
1      1     A  white 1
2      2     A  black 1
3      3     A  white 2
4      4     B  white 1
5      5     B  black 1
6      6     B  white 2
7      7     B  ...   2
8      8     B        3
9      9     B        3
10    10     B        4
11    11     B        4
12    12     B        5 
13    13     B        5
14    14     B        6
15    15     B        6
16    16     B        7
17    17     B        7
18    18     C  white 1
19    19     C  black 1
20    20     C  white 2
21    21     C  ...   2
22    22     C        3
23    23     C        3

CodePudding user response:

You may want to use the following:

library(dplyr)

df <- data.frame(value = 1:23)

df %>% 
  mutate(group = case_when(value < 4 ~ "A",
                           value >= 4 &  value < 18 ~ "B",
                           value >= 18 & value <= 23 ~ "C")) %>% 
  group_by(group) %>% 
  mutate(color = if_else(row_number() %% 2 == 1, "white", "black"),
         distance = rep(1:n(), each =2)[1:n()]) %>% 
  ungroup

#> # A tibble: 23 × 4
#>    value group color distance
#>    <int> <chr> <chr>    <int>
#>  1     1 A     white        1
#>  2     2 A     black        1
#>  3     3 A     white        2
#>  4     4 B     white        1
#>  5     5 B     black        1
#>  6     6 B     white        2
#>  7     7 B     black        2
#>  8     8 B     white        3
#>  9     9 B     black        3
#> 10    10 B     white        4
#> # … with 13 more rows
  • Related