Reputation: 3
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
Upvotes: 0
Views: 71
Reputation: 73004
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"))
Upvotes: 0
Reputation: 4150
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)
Upvotes: 1