Home > Software engineering >  How to expand a dataframe in R with a continuous variable?
How to expand a dataframe in R with a continuous variable?

Time:10-10

I have this dataset:

group_ask <- c('A', 'A', 'B', 'B', 'C', 'C')
number_ask <- c(1, 3, 2, 4, 5, 8)
df_ask <- data.frame(group_ask, number_ask)

I am trying to expand the group_ask column by completing the continuous number_ask column. The solution dataset should look like this:

group_want <- c('A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C')
number_want <- c(1, 2, 3, 2, 3, 4, 5, 6, 7, 8)
df_want <- data.frame(group_want, number_want)

I have unsuccessfully been trying to solve this R's expand() function.

Any suggestions? Many thanks!

CodePudding user response:

You may use complete -

library(dplyr)
library(tidyr)

df_ask %>%
  group_by(group_ask) %>% 
  complete(number_ask = min(number_ask):max(number_ask)) %>%
  ungroup

#  group_ask number_ask
#   <chr>          <dbl>
# 1 A                  1
# 2 A                  2
# 3 A                  3
# 4 B                  2
# 5 B                  3
# 6 B                  4
# 7 C                  5
# 8 C                  6
# 9 C                  7
#10 C                  8

CodePudding user response:

Split apply combine approach using by.

do.call(rbind.data.frame, 
        by(df_ask, df_ask$group_ask, \(x) 
           cbind(x[1, 1], do.call(seq, as.list(x[, 2]))))) |>
  setNames(names(df_ask))
#     group_ask number_ask
# A.1         A          1
# A.2         A          2
# A.3         A          3
# B.1         B          2
# B.2         B          3
# B.3         B          4
# C.1         C          5
# C.2         C          6
# C.3         C          7
# C.4         C          8
  • Related