0

Suppose the following data structure:

structure(list(`1.a` = c("a", NA, "a"), `1.b` = c("b", "b", NA
), `2` = c("ba", "ba", "ab"), `3.a` = c("a", "a", NA), `3.b` = c("b", 
NA, "b")), row.names = c(NA, -3L), class = c("tbl_df", "tbl", 
"data.frame"))

# A tibble: 3 x 5
  `1.a` `1.b` `2`   `3.a` `3.b`
  <chr> <chr> <chr> <chr> <chr>
1 a     b     ba    a     b    
2 NA    b     ba    a     NA   
3 a     NA    ab    NA    b  

Now, for the columns with .a etc., I want to create a column named X (the part/number before the dot) in front of the .a columns and paste the cell values together, keeping the "order". Result I want:

# A tibble: 3 x 7
  `1`   `1.a` `1.b` `2`   `3`   `3.a` `3.b`
  <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 ab    a     b     ab    ab    a     b    
2 b     NA    b     a     a     a     NA   
3 a     a     NA    b     b     NA    b  

persephone
  • 380
  • 2
  • 10
  • Does this answer your question? [Sort columns of a dataframe by column name](https://stackoverflow.com/questions/7334644/sort-columns-of-a-dataframe-by-column-name) – MrGumble Feb 19 '21 at 14:44

1 Answers1

1

A base R option

do.call(
  cbind,
  unname(
    lapply(
      split.default(df, gsub("\\..*", "", names(df))),
      function(x) {
        if (length(x) > 1) {
          cbind(
            setNames(
              data.frame(
                apply(x, 1, function(v) paste0(na.omit(unlist(v)), collapse = ""))
              ),
              unique(gsub("\\..*", "", names(x)))
            ),
            x
          )
        } else {
          x
        }
      }
    )
  )
)

gives

   1  1.a  1.b  2  3  3.a  3.b
1 ab    a    b ba ab    a    b
2  b <NA>    b ba  a    a <NA>
3  a    a <NA> ab  b <NA>    b
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81