how to create a list column of column names where column values are TRUE

mac

I've got some survey data for multiple-selection questions that has been output with a column for each possible selection, populated with TRUE/FALSE values, e.g.

library(dplyr)

dat <- tribble(
  ~name, ~state, ~onions, ~`sweet potatoes`, ~garlic,
  "Tom", "WV", TRUE, FALSE, TRUE,
  "Larry", "NC", FALSE, TRUE, FALSE,
  "Beth", "NY", TRUE, TRUE, TRUE
)

dat

#>   name  state onions `sweet potatoes` garlic
#>   <chr> <chr> <lgl>  <lgl>            <lgl> 
#> 1 Tom   WV    TRUE   FALSE            TRUE  
#> 2 Larry NC    FALSE  TRUE             FALSE 
#> 3 Beth  NY    TRUE   TRUE             TRUE  

How can I create a list-column containing the column names for which the respondents answered "TRUE"?

simliar to https://stackoverflow.com/a/9508203/1009730

I've tried several variations on the theme of

dat %>%
  rowwise() %>%
  mutate(fav_foods = list(names(which(~ is.logical(.x) && .x))))

which gives the error:

Error: Problem with mutate() input fav_foods. x argument to 'which' is not logical i Input fav_foods is list(names(which(~is.logical(.x) && .x))). i The error occurred in row 1.

tmfmnk

With the addition of purrr, you could do:

dat %>%
 mutate(fav_foods = pmap_chr(across(where(is.logical)), ~ toString(names(c(...))[which(c(...))])))

  name  state onions `sweet potatoes` garlic fav_foods                     
  <chr> <chr> <lgl>  <lgl>            <lgl>  <chr>                         
1 Tom   WV    TRUE   FALSE            TRUE   onions, garlic                
2 Larry NC    FALSE  TRUE             FALSE  sweet potatoes                
3 Beth  NY    TRUE   TRUE             TRUE   onions, sweet potatoes, garlic

Or if you need it as a list:

dat %>%
 mutate(fav_foods = pmap(across(where(is.logical)), ~ names(c(...))[which(c(...))]))

  name  state onions `sweet potatoes` garlic fav_foods
  <chr> <chr> <lgl>  <lgl>            <lgl>  <list>   
1 Tom   WV    TRUE   FALSE            TRUE   <chr [2]>
2 Larry NC    FALSE  TRUE             FALSE  <chr [1]>
3 Beth  NY    TRUE   TRUE             TRUE   <chr [3]>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to create a list column of column names where column values are TRUE

From Dev

Pandas: Compress Column Names to Cell Values where True

From Dev

How to create a new column with names in a list

From Dev

pandas create column with names from values and substitute with True/False

From Java

How to create column that identifies another column where the row values change?

From Dev

How to convert column values to column names in teradata?

From Dev

How to convert column values to column names in teradata?

From Dev

Create new columns in a data frame based on an existing numeric column, a list of strings as column names and a list of tuples as values

From Dev

How to find row names where values are maximum in a column in Pyspark

From Dev

How to find row names where values are maximum in a column in Pyspark

From Dev

Selecting column names where values are NULL

From Dev

Reshape column values to column names

From Dev

How to get column names and and their values to array tuple list with Entity Framework

From Dev

How to extract unique values from pandas column where values are in list

From Dev

How to create "group" column by values in column

From Dev

Create new names to column based on lookup values

From Dev

How to figure out the column names programmatically using Netezza SQL, where the column values matches a preset value

From Dev

How to figure out the column names programmatically using Netezza SQL, where the column values matches a preset value

From Dev

SAS - Dynamically create column names using the values from another column

From Dev

Create one hot column names based on column values in pandas

From Dev

Create a new column based on other column names and values

From Dev

Passing a list of column names and values to Psycopg

From Dev

Provide a list of names with consecutive values in another column

From Dev

how to create a pandas DataFrame by combining a list of column_names and a numpy array, and then adding more column(s)?

From Dev

Changing column names and values

From Dev

Turn values into column names

From Dev

Column names to values

From Dev

how to add dataframes where the column names are same

From Dev

How to convert row values as column names and same column names as column values in python?

Related Related

  1. 1

    how to create a list column of column names where column values are TRUE

  2. 2

    Pandas: Compress Column Names to Cell Values where True

  3. 3

    How to create a new column with names in a list

  4. 4

    pandas create column with names from values and substitute with True/False

  5. 5

    How to create column that identifies another column where the row values change?

  6. 6

    How to convert column values to column names in teradata?

  7. 7

    How to convert column values to column names in teradata?

  8. 8

    Create new columns in a data frame based on an existing numeric column, a list of strings as column names and a list of tuples as values

  9. 9

    How to find row names where values are maximum in a column in Pyspark

  10. 10

    How to find row names where values are maximum in a column in Pyspark

  11. 11

    Selecting column names where values are NULL

  12. 12

    Reshape column values to column names

  13. 13

    How to get column names and and their values to array tuple list with Entity Framework

  14. 14

    How to extract unique values from pandas column where values are in list

  15. 15

    How to create "group" column by values in column

  16. 16

    Create new names to column based on lookup values

  17. 17

    How to figure out the column names programmatically using Netezza SQL, where the column values matches a preset value

  18. 18

    How to figure out the column names programmatically using Netezza SQL, where the column values matches a preset value

  19. 19

    SAS - Dynamically create column names using the values from another column

  20. 20

    Create one hot column names based on column values in pandas

  21. 21

    Create a new column based on other column names and values

  22. 22

    Passing a list of column names and values to Psycopg

  23. 23

    Provide a list of names with consecutive values in another column

  24. 24

    how to create a pandas DataFrame by combining a list of column_names and a numpy array, and then adding more column(s)?

  25. 25

    Changing column names and values

  26. 26

    Turn values into column names

  27. 27

    Column names to values

  28. 28

    how to add dataframes where the column names are same

  29. 29

    How to convert row values as column names and same column names as column values in python?

HotTag

Archive