0

I am looking to merge across two columns which contain different data. That is where it is empty in one column it is not empty in another, essentially data collected from two different conditions in a study, in order to run analysis I need to combine this data and run t-tests. I was wondering how to combine numerical columns with dplyr -

check <- check %>% 
  mutate(cat = rowSums(.[1:2]))


> dput(head(check))
structure(list(t7_1_ExpA_Intro1 = c(NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_), t7_1_ExpB_Intro1 = c(NA, 
NA, NA, 3L, NA, NA), t7_1_ExpA_DV = c(NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_), t7_1_ExpB_DV = c(NA, 
NA, NA, 3L, NA, NA), cat = c(NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_)), .Names = c("t7_1_ExpA_Intro1", 
"t7_1_ExpB_Intro1", "t7_1_ExpA_DV", "t7_1_ExpB_DV", "cat"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
Lowpar
  • 897
  • 10
  • 31
  • 1
    It would be easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the desired output. – MrFlick Jul 24 '17 at 20:25
  • When there's no data, it's `NA` or `0`? You can use `max` function – M-- Jul 24 '17 at 20:26
  • `check$cat <- apply(check[,1:2], 1, max)` This should work for both cases. – M-- Jul 24 '17 at 20:28
  • @Masoud I tried, it but it did not work, strange though, seems like it should work, also the values are NA – Lowpar Jul 24 '17 at 20:40
  • can you check class of your data if it's numeric? Provide this, and you question will be answered momentarily: `dput(head(check))`. – M-- Jul 24 '17 at 20:41
  • Updated now, sorry about all the hassle – Lowpar Jul 24 '17 at 20:42

1 Answers1

5

You can use dplyr::coalesce function which selects values from columns that first appear as non NA; say if you want to combine the first two columns:

check %>% mutate(cat = coalesce(t7_1_ExpA_Intro1, t7_1_ExpB_Intro1))
# or check %>% mutate(cat = do.call(coalesce, .[1:n])) if you have more columns to coalesce

# A tibble: 6 x 5
#  t7_1_ExpA_Intro1 t7_1_ExpB_Intro1 t7_1_ExpA_DV t7_1_ExpB_DV   cat
#             <int>            <int>        <int>        <int> <int>
#1               NA               NA           NA           NA    NA
#2               NA               NA           NA           NA    NA
#3               NA               NA           NA           NA    NA
#4               NA                3           NA            3     3
#5               NA               NA           NA           NA    NA
#6               NA               NA           NA           NA    NA
Psidom
  • 209,562
  • 33
  • 339
  • 356