I have a dataframe that looks like this
Name Cricket Football Swimming
A Y Y N
B N Y N
C Y N
Something like this (in base R) would do it too:
df$Sports <- apply(df[,-1]=="Y", 1, function(r) paste(names(df)[-1][r], collapse=" and "))
# Name Cricket Football Swimming Sports
#1 A Y Y N Cricket and Football
#2 B N Y N Football
#3 C Y N Y Cricket and Swimming
An option would be to gather
the data into 'long' format, filter
the rows with 'Y', grouped by 'Name', paste the elements in 'key' (str_c
) and left_join
with the original dataset
library(tidyverse)
df1 %>%
gather(key, val, -Name) %>%
filter(val == 'Y') %>%
group_by(Name) %>%
summarise(Sports = str_c(key, collapse= ' and ')) %>%
left_join(df1) %>%
select(names(df1), "Sports")
# A tibble: 3 x 5
# Name Cricket Football Swimming Sports
# <chr> <chr> <chr> <chr> <chr>
#1 A Y Y N Cricket and Football
#2 B N Y N Football
#3 C Y N Y Cricket and Swimming
df1 <- structure(list(Name = c("A", "B", "C"), Cricket = c("Y", "N",
"Y"), Football = c("Y", "Y", "N"), Swimming = c("N", "N", "Y"
)), class = "data.frame", row.names = c(NA, -3L))