Home > OS >  Add levels in one column for each level of a factor in another column
Add levels in one column for each level of a factor in another column

Time:04-09

I have a dataset like this:

dat<- data.frame(var1= c(1:15), 
                var2= c(rep("A", 4), "control",  rep("B", 4),"control", rep("C", 4), "control"),
                var3= rep(c("full","full", "half", "half", "control"),3),
                value= c(12, 62, 60, 61, 83, 97,1, 22, 99, 47, 63, 49, 25, 81, 26))
dat

I for each level(var3)=="control" I want to add those rows to each level of dat$var2. The result data frame would look like:

res<- data.frame(var1= c(1:21), 
             var2= c(rep("A", 7),  rep("B", 7), rep("C", 7)),
             var3= rep(c("full","full", "half", "half", "control","control", "control"),3),
             value= c(12, 62, 60, 61, 83, 47, 26,97,1, 22, 99,83, 47, 26, 63, 49, 25, 81,83, 47,26))

res

CodePudding user response:

Here is one option

library(dplyr)
datsub <- dat %>% 
   filter(var3 == "control") %>% 
   select(var3, value)
out <- dat %>% 
  filter(var3 != 'control') %>%
  group_by(var2) %>% 
  group_modify(~ .x %>% bind_rows(datsub))  %>%
  ungroup %>%
  mutate(var1 = row_number()) %>%
  select(names(dat))

-checking with expected output

> all.equal(out, res, check.attributes = FALSE)
[1] TRUE

CodePudding user response:

A similar approach, but a little different:

dat_to_augment = dat %>% filter(var3 != "control")

dat %>%
  filter(var3 == "control") %>%
  select(var3, value) %>%
  full_join(distinct(dat_to_augment, var2), by = character()) %>%
  bind_rows(dat_to_augment) %>%
  select(var1, var2, var3, value) %>%
  arrange(var2, desc(var3)) %>%
  mutate(var1 = row_number())
#    var1 var2    var3 value
# 1     1    A    half    60
# 2     2    A    half    61
# 3     3    A    full    12
# 4     4    A    full    62
# 5     5    A control    83
# 6     6    A control    47
# 7     7    A control    26
# 8     8    B    half    22
# 9     9    B    half    99
# 10   10    B    full    97
# ...
  • Related