Home > Mobile >  Create repeating number of length n for each group in a dataframe
Create repeating number of length n for each group in a dataframe

Time:10-11

Suppose I have the following dataset and I want to add repeating numbers of length n starting from 1 to each group. In this example, n is set to 3.

test <- data.frame("group" = c(rep("AA",5),rep("BB",3)), "value" = 12:19)
  group value
1    AA    12
2    AA    13
3    AA    14
4    AA    15
5    AA    16
6    BB    17
7    BB    18
8    BB    19

I would like to turn the data frame to the following given that I specify the repeating number (n) is 3.

  group value group_n
1    AA    12    AA_1
2    AA    13    AA_1
3    AA    14    AA_1
4    AA    15    AA_2
5    AA    16    AA_2
6    BB    17    BB_1
7    BB    18    BB_1
8    BB    19    BB_1

Any help is appreciated. Thank you!

CodePudding user response:

We could use length.out in rep, after grouping by 'group'

library(data.table)
n <- 3
setDT(test)[, group_n := paste0(.BY, "_", rep(seq_len(.N),
     each = n, length.out = .N)), by = group]

-output

> test
    group value group_n
   <char> <int>  <char>
1:     AA    12    AA_1
2:     AA    13    AA_1
3:     AA    14    AA_1
4:     AA    15    AA_2
5:     AA    16    AA_2
6:     BB    17    BB_1
7:     BB    18    BB_1
8:     BB    19    BB_1

CodePudding user response:

A base R option using ave ceiling (without rep)

transform(
  test,
  group_n = ceiling(ave(seq_along(value), group, FUN = seq_along) / 3)
)

gives

  group value group_n
1    AA    12       1
2    AA    13       1
3    AA    14       1
4    AA    15       2
5    AA    16       2
6    BB    17       1
7    BB    18       1
8    BB    19       1

CodePudding user response:

Using dplyr, group_by and integer division...

library(dplyr)

n <-  3

test |>
  group_by(group) |> 
  mutate(group_n = paste(group, (row_number() n-1) %/% n, sep = "_"))

#> # A tibble: 8 × 3
#> # Groups:   group [2]
#>   group value group_n
#>   <chr> <int> <chr>  
#> 1 AA       12 AA_1   
#> 2 AA       13 AA_1   
#> 3 AA       14 AA_1   
#> 4 AA       15 AA_2   
#> 5 AA       16 AA_2   
#> 6 BB       17 BB_1   
#> 7 BB       18 BB_1   
#> 8 BB       19 BB_1

Created on 2022-10-10 with reprex v2.0.2

  • Related