0

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

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0

One way using table().

with(dat, table(Name, code)) |> 
  apply(1:2, \(x) ifelse(x == 1, 'YES', NA)) |>
  data.frame(Name=unique(dat$Name))
#       FI   IT   NO   RO   RU   SE Name
# ABC  YES <NA>  YES <NA>  YES  YES  ABC
# BCD <NA>  YES <NA>  YES <NA> <NA>  BCD

Note: R >= 4.1 used.


Data:

dat <- structure(list(Name = c("ABC", "ABC", "ABC", "ABC", "BCD", "BCD"
), code = c("FI", "NO", "RU", "SE", "IT", "RO")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.