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 :
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)