0

I would like to create a new column which appends the suffix of column names together separated by space if the value is "TRUE". For example if I have this dataset:

df <- data.frame(
  "category/var1" = c(TRUE, FALSE, TRUE),
  "category/var2" = c(FALSE, TRUE, FALSE),
  "category/var3" = c(TRUE, TRUE, FALSE)
)

# Print the dataframe
print(df)

  category/var1 category/var2 category/var3
1          TRUE         FALSE          TRUE
2         FALSE          TRUE          TRUE
3          TRUE         FALSE         FALSE

I want to identify columns which have the same prefix "category/", and create a new column named "category" that appends the suffix of all column names if the values in that row are "TRUE".

I want the resulting new column to look like this:

  category/var1 category/var2 category/var3   category
1          TRUE         FALSE          TRUE var1  var3
2         FALSE          TRUE          TRUE     var2
3          TRUE         FALSE         FALSE     var1

But I cannot seem to do this in an elegant way.

  • This answer from @r2evans I found in another thread worked for me! df %>% rowwise() %>% mutate( category= paste(sort( apply(pick(starts_with("category/")), 1, \(z) names(which(z == "TRUE"))) ), collapse = ", ") ) %>% ungroup() Then I added this to remove prefix: %>% mutate(category = gsub("category/", "", category)) https://stackoverflow.com/questions/76623015/creating-new-column-using-existing-column-names-and-values – Nara McCray Jul 06 '23 at 11:43

1 Answers1

0

Please try the below code

df %>% mutate(row=row_number()) %>% pivot_longer(cols = c(everything(),-row)) %>% 
  mutate(new=ifelse(value==T,str_extract(name,'(?<=\\.).*'),NA),
         new2=paste0(na.omit(new), collapse = ','),
         .by=row) %>% pivot_wider(id_cols = new2, names_from = name, values_from = value)

Created on 2023-07-05 with reprex v2.0.2

# A tibble: 3 × 4
  new2      category.var1 category.var2 category.var3
  <chr>     <lgl>         <lgl>         <lgl>        
1 var1,var3 TRUE          FALSE         TRUE         
2 var2,var3 FALSE         TRUE          TRUE         
3 var1      TRUE          FALSE         FALSE        

jkatam
  • 2,691
  • 1
  • 4
  • 12