consolidate the various rows

One possible solution if there is allways a second line would be this:

library(dplyr)

library(dplyr)

df %>% 
  # use the row number as colum
  dplyr::mutate(ID = dplyr::row_number()) %>% 
  # substract 1 from very even row numer to build groups
  dplyr::mutate(ID = ifelse(ID %% 2 == 0, ID - 1, ID)) %>% 
  # group by the new ID
  dplyr::group_by(ID) %>% 
  # convert all NAs to "" (empty string)
  dplyr::mutate_all(~ ifelse(is.na(.), "", .)) %>% 
  # concatenate all strings per group
  dplyr::mutate_all( ~ paste(., collapse = "")) %>% 
  # select only distinct cases (do elimitate "seconds" as the now are identical to "frists)
  dplyr::distinct()


  V1    V2    V3       ID
  <chr> <chr> <chr> <dbl>
1 4     32    4         1

I left the created ID number in the result but you can drop/delete it after the calculations if you prefer

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top