1

I merged various dataframes in R, which had variables with the same name. In the merged file I got variables names as varA, varA.x, varA.x1, varA.x.y, etc. I want to create a file merging the content of all these variables in a single column. As an example of my file:

ID weight age varA varA.x varA.x.y varA.x.y.1
1    50    30  2     NA      NA        NA
2    78    34  NA     3      NA        NA
3    56    56  NA     NA     NA        6
4    56    67  NA     NA     7         NA

I want a file that looks like:

ID weight age varA
1   50    30   2
2    78    34  3
3    56    56  6
4    56    67  7

It is not feasible to use ifelse: `data$varA = ifelse(is.na(varA.x),varA.y,varA.x), because the statement would be too long as I have so many repeated variables.

Can you help me, please? Thank you so much.

PaulaF
  • 393
  • 3
  • 17

1 Answers1

1

We can use coalesce from tidyr

library(tidyverse)
df1 %>%
   mutate(varA = coalesce(varA, varA.x, varA.x.y, varA.x.y.1)) %>% 
   select_(.dots = names(.)[1:4])
#   ID weight age varA
#1  1     50  30    2
#2  2     78  34    3
#3  3     56  56    6
#4  4     56  67    7

Or use pmax from base R

cbind(df1[1:3], varA=do.call(pmax, c(df1[grep("varA", names(df1))], na.rm = TRUE)))
akrun
  • 874,273
  • 37
  • 540
  • 662