Home > database >  R Add rows to each group so each group has same number, and specify other variable
R Add rows to each group so each group has same number, and specify other variable

Time:02-17

This is my df:

df = tibble(week = c(1,1,2,2,3,3,3,4,4,4,4),
             session = c(1,2,1,2,1,2,3,1,2,3,4),
             work =rep("done",11))
df
# A tibble: 11 x 3
    week session work 
   <dbl>   <dbl> <chr>
 1     1       1 done 
 2     1       2 done 
 3     2       1 done 
 4     2       2 done 
 5     3       1 done 
 6     3       2 done 
 7     3       3 done 
 8     4       1 done 
 9     4       2 done 
10     4       3 done 
11     4       4 done 

For each week there should be 4 rows with session 1 to 4. How can I add the "missing" session rows (the rest of the variables are NA) so the df is:

df1= tibble(week =  c(rep(1,4), rep(2,4), rep(3,4), rep(4,4)),
            session = rep(1:4,4),
            work = c("done", "done" ,NA, NA, "done", "done" ,NA, NA,"done", "done" ,"done", NA, rep("done",4)))
df1
  week session work 
   <dbl>   <int> <chr>
 1     1       1 done 
 2     1       2 done 
 3     1       3 NA   
 4     1       4 NA   
 5     2       1 done 
 6     2       2 done 
 7     2       3 NA   
 8     2       4 NA   
 9     3       1 done 
10     3       2 done 
11     3       3 done 
12     3       4 NA   
13     4       1 done 
14     4       2 done 
15     4       3 done 
16     4       4 done 

CodePudding user response:

tidyr::complete(df, week, session)

# A tibble: 16 x 3
    week session work 
   <dbl>   <dbl> <chr>
 1     1       1 done 
 2     1       2 done 
 3     1       3 NA   
 4     1       4 NA   
 5     2       1 done 
 6     2       2 done 
 7     2       3 NA   
 8     2       4 NA   
 9     3       1 done 
10     3       2 done 
11     3       3 done 
12     3       4 NA   
13     4       1 done 
14     4       2 done 
15     4       3 done 
16     4       4 done 

CodePudding user response:

Here's a data.table solution in case speed is important

# load package
library(data.table)

# set as data table
setDT(df)

# cross join to get complete combination
week <- 1:4
session <- 1:4
z <- CJ(week,session)

# join
df_1 <- df[z, on=.(week, session)]
  • Related