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

Is there a way to loop R functions across multiple columns when the function is determined by the column name?

I have a dataset that I am converting from long to wide format. My original dataset contains countries of occurrence for each species ("code") as one of the columns. I have created new columns for all country codes (e.g., "FI", "NO"), and I now need to translate the data in "code" to these columns (e.g., for df$FI, if df$Name == "FI" then YES, otherwise NA). Is there a way of automating this across all of these new columns (so that a YES value will be returned if the value in df$Name is equivalent to the column title, otherwise an NA value will be returned), or do I have to do this manually for each new column created? Thanks.

   Name        code
1   ABC         FI
2   ABC         NO
3   ABC         RU
4   ABC         SE
5   BCD         IT
6   BCD         RO

>Solution :

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

Here is a somewhat strange solution

library(tidyverse)


example_data <- read_table('Name        code
ABC         NO
ABC         RU
ABC         SE
BCD         IT
BCD         RO')

example_data |> 
  pivot_wider(names_from = code,values_from = code) |> 
  mutate(across(-Name,.fns = ~ if_else(.x |> is.na(),
                                       NA_character_,
                                       'YES')))
#> # A tibble: 2 x 6
#>   Name  NO    RU    SE    IT    RO   
#>   <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 ABC   YES   YES   YES   <NA>  <NA> 
#> 2 BCD   <NA>  <NA>  <NA>  YES   YES

Created on 2022-01-24 by the reprex package (v2.0.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