0

I'm trying to do analysis from multiple csv files, and in order to create a key that can be used for left_join I think that I need to try and merge two columns. At present I'm trying to use the tidyverse packages (inc. mutate), but I'm running into an issue as the two columns to merge have different formatting: 1 is a double and the other is in date format. I'm using the following code

 qlik2 <- qlik %>%
  separate('Admit DateTime', into = c('Admit Date', 'Admit Time'), sep = 10) %>%
  mutate(key = MRN + `Admit Date`)

and getting tis output error:

Error in mutate_impl(.data, dots) : 
  Evaluation error: non-numeric argument to binary operator.

If there's another way around this (or if the error is actually related to something else), then I'd appreciate any thoughts on the matter. Equally, if people know of a way to left_join with multiple keys, then that would work as well.

Thanks, Cal

arnold-c
  • 337
  • 2
  • 13
  • What is the `MRN` variable? You're getting that error because you're trying to use `+` on non numeric variables. Regarding `left_join`, you can use the `by` argument to declare the desired keys as you put it. `dplyr::left_join(df1, df2, by = c("var1", "var2", "varN"))` – On_an_island Dec 08 '18 at 03:58
  • MRN is just a number. I'm afraid that when I used `qlik4 <- qlik2 %>% + left_join(cxr, by = c('MRN', 'Admit Date'))` I got the error `Error in left_join_impl(x, y, by_x, by_y, aux_x, aux_y, na_matches) : cannot join a Date object with an object that is not a Date object` even though `MRN` and `Admit Date` are variables in both tibbles. – arnold-c Dec 08 '18 at 17:34
  • The error results from trying to join a variable of class `Date` with a non-date variable. `Admit Date` variable needs to be of class `Date` in both data frames. – On_an_island Dec 08 '18 at 17:39
  • Ah, yep, that was the problem. Thanks for the help – arnold-c Dec 08 '18 at 19:07

1 Answers1

1

Hard without a reproducible example. But if i understand your question you either want a numeric key, or trying to concatinate a string with the plus +.

Numeric key

library(hablar)

qlik2 <- qlik %>%
  separate('Admit DateTime', 
           into = c('Admit Date', 'Admit Time'), 
           sep = 10) %>%
  convert(num(MRN, `Admit Date`)) %>% 
  mutate(key = MRN + `Admit Date`)

String key

qlik2 <- qlik %>%
  separate('Admit DateTime', 
           into = c('Admit Date', 'Admit Time'), 
           sep = 10) %>%
  mutate(key = paste(MRN, `Admit Date`))
davsjob
  • 1,882
  • 15
  • 10