Merge cells horizontally with datatable in R

49 views Asked by At

I would like to merge cells horizontally (merge 2 columns) on one row.

Minimum, reproducible example:

datatable(data.frame('A' = c('a', 1),
                     'B' = c('bc', 2),
                     'C' = c('', 3),
                     'D' = c('de', 4),
                     'E' = c('', 5)))

enter image description here

Desired output:

enter image description here

Thank you!

1

There are 1 answers

1
Isaac On

Maybe this is one option to do that:

> data.frame(
+   'A' = c('a', 1),
+   'B' = c('bc', 2),
+   'C' = c('', 3),
+   'D' = c('de', 4),
+   'E' = c('', 5)
+ ) |> 
+   rowwise() |> 
+   mutate(C = B, E = D)
# A tibble: 2 × 5
# Rowwise: 
  A     B     C     D     E    
  <chr> <chr> <chr> <chr> <chr>
1 a     bc    bc    de    de   
2 1     2     2     4     4