Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mutate one column by filtering others in dplyr

I have a dataset with several columns that are mutually exclusive. One of the columns contains the answer for that observation (e.g. "abc") while the rest of columns contains negative numbers that represent NA. As column A in the toy dataset below, a column always contains the same answer .

I want to create a new variable by merging the column with the answer for each observation. So far, I did it as below, but in the real dataset there are 50 columns for 2000 observations, so I’m looking for a more optimized (and more elegant) way to do it.

data <- tibble::tribble(
  ~id, ~A, ~B, ~C,
  "a", "abc", "-1", "-2",
  "b", "-1","-3", "def",
  "c", "-2", "ghi", "-1",
  "d", "abc", "-3", "-1"
)

data %>% 
  dplyr::mutate(across(A:C, ~ ifelse(str_starts(.,"-"), "", .))) %>%
  dplyr::mutate(regions = paste0(A, B, C))

Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We may use unite from tidyr and then remove the numeric/- characters with str_remove_all

library(dplyr)
library(tidyr)
library(stringr)
data %>%
  unite(regions, A:C, remove = FALSE) %>%
  mutate(regions = str_remove_all(regions, "[^a-z]+"))

-output

# A tibble: 4 × 5
  id    regions A     B     C    
  <chr> <chr>   <chr> <chr> <chr>
1 a     abc     abc   -1    -2   
2 b     def     -1    -3    def  
3 c     ghi     -2    ghi   -1   
4 d     abc     abc   -3    -1   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading