-1

I am trying to do something that I think is straightforward but I am having an issue with.

I have several medication-related column variables (med_1, med_2, med_3 for example). These are character variables- so they have text for the name of medications

I want to combine them all into variable anymed using or logic, so that I can then use anymed to look at any medications reported across all medication related fields.

I am trying the following, for dataset FinalData.

FinalData <- FinalData %>% mutate(anymed = med_1 | med_2 | med_3)

I am receiving this error:

*Error: Problem with `mutate()` column `anymed`.
ℹ `anymed = |...`.
x operations are possible only for numeric, logical or complex types*

Could someone help explain what code I should use instead since these are characters? Do I need to convert to factors?

Kate G
  • 1
  • 1
  • 2
    [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. That includes a sample of data and the output you're trying to get – camille Jan 08 '22 at 19:59

2 Answers2

0

You want to use pivot_longer from tidyverse to get them all in the same column. I also dropped the column name (i.e., col), but you could remove that line if you want to know what column the medication came from. I'm unsure what your data looks like, so I just made a small example to show how to do it.

library(tidyverse)

FinalData %>%
  pivot_longer(-ind, names_to = "col", values_to = "anymed") %>%
  select(-col)

Output

# A tibble: 6 × 2
    ind anymed
  <dbl> <chr> 
1     1 meda  
2     1 meda  
3     1 meda  
4     2 medb  
5     2 medb  
6     2 medb  

It's a little unclear what your expected output is. But if you are wanting to combine all medications in each row, then you can also use unite.

FinalData %>%
  unite("any_med", c("med_1", "med_2", "med_3"), sep = " | ")

Output

  ind            any_med
1   1 meda | meda | meda
2   2 medb | medb | medb

Data

FinalData <-
  structure(
    list(
      ind = c(1, 2),
      med_1 = c("meda", "medb"),
      med_2 = c("meda",
                "medb"),
      med_3 = c("meda", "medb")
    ),
    class = "data.frame",
    row.names = c(NA,-2L)
  )
AndrewGB
  • 16,126
  • 5
  • 18
  • 49
0

Are you looking for this kind of solution:

# data:
df <- tibble(med_1 = "A", med_2 = "B", med_3 = "C")

library(dplyr)
df %>% 
  mutate(any_med = paste(c(med_1, med_2, med_3), collapse = " | "))
  med_1 med_2 med_3 any_med  
  <chr> <chr> <chr> <chr>    
1 A     B     C     A | B | C
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • I guess I was unclear if they wanted them all in the same column or wanted a `rowwise` solution. – AndrewGB Jan 08 '22 at 19:51
  • 1
    I am not sure. We will see! :-) – TarJae Jan 08 '22 at 19:52
  • Hi! Thank you. Med_1, Med_2, and Med_3 are columns, each with ~40 medications. Each row is a unique combination of patient+visit#. I think rowwise works but I am not sure about your first line of code (where med_1 = "A" for example), since med_1 = ~40 values. Would I need to write out all of them? – Kate G Jan 08 '22 at 21:50
  • Do the following: type in your console: `dput(head(here the name of your dataframe))` then copy the output and add it to your question. Ideally you could construct a desired output. This is the way your chances to get what you desire increase rapidly! – TarJae Jan 08 '22 at 21:56