Search code examples
rstatisticspivottidyversetidyr

How to separate or unnest several variables in a character column


I have a multiple choice question from my survey that has several character variables per line. I was trying to figure out how I can separate these using tidyverse, so I can use ggplot2 to put the total answers for each variable in a bar graph.

Example:

What are your preferred methods for receiving information about precision technologies and conservation programs and incentives? (Select up to 3)

df <- data.frame (Name  = c("Sue", "Joe", "Bob"),
                  Q3 = c("Mail,Live Demonstrations,Websites", "Mail,Websites,In-person meetings/seminars", "Email,Mail,Videos (Ex: Youtube)"))

I tried pivot_longer(Survey$Q3) (with Q3 being the answers) but I got back this error

"Error in UseMethod("pivot_longer") : 
  no applicable method for 'pivot_longer' applied to an object of class "character""

Solution

  • You can str_split the column then unnest:

    df %>%
      as_tibble() %>%
      mutate(Q3 = str_split(Q3, ",")) %>%
      unnest(Q3)
    
    # # A tibble: 9 × 2
    #   Name  Q3                         
    #   <chr> <chr>                      
    # 1 Sue   Mail                       
    # 2 Sue   Live Demonstrations        
    # 3 Sue   Websites                   
    # 4 Joe   Mail                       
    # 5 Joe   Websites                   
    # 6 Joe   In-person meetings/seminars
    # 7 Bob   Email                      
    # 8 Bob   Mail                       
    # 9 Bob   Videos (Ex: Youtube)