Rapidly find the mean of survey questions

Following on from the last blog post, I’ve got quite a nice way of generating lots of means from a survey dataset. This one relies on the fact that I’m averaging questions that go 2.1, 2.2, 2.3, and 3.1, 3.2, 3.3, so I can look for all questions that start with “2.”, “3.”, etc.

library(tidyverse)

survey <- survey %>% bind_cols(
  
  map(paste0(as.character(2 : 9), "."), function(x) {
    
    return_value <- survey %>% 
      select(starts_with(x)) %>% 
      rowMeans(., na.rm = TRUE) %>% 
      as.data.frame()
    
    names(return_value) <- paste0("Question ", x)
    
    return(return_value)
  }) 
)

This returns the original dataset plus the averages of each question, labelled “Question 2”, “Question 3”, etc.

Converting words on a survey dataset to numbers for analysis

As always, I have very little time for blogging (sorry) but I just came up with a neat way of converting “Strongly Agree”, “Always”, all that stuff that you get on survey based datasets into numbers ready for analysis. It’s automatic, so it will play havoc with any word based questions- analyse them in a separate script.

Here it is

library(tidyverse)

survey <- map_df(survey, function(x) {
  
  if(sum(grepl("Agree", unlist(x), ignore.case = TRUE)) > 0) {
    
    return(case_when(
      x == "Strongly agree" ~ 10,
      x == "Agree" ~ 7.5,
      x == "Neither agree nor disagree" ~ 5,
      x == "Disagree" ~ 2.5,
      x == "Strongly disagree" ~ 0,
      TRUE ~ NA_real_
    ))
  } else if(sum(grepl("Always", unlist(x), ignore.case = TRUE)) > 0){
    
    return(case_when(
      x == "Always" ~ 10,
      x == "Often" ~ 7.5,
      x == "Sometimes" ~ 5,
      x == "Rarely" ~ 2.5,
      x == "Never" ~ 0,
      TRUE ~ NA_real_
    ))
  
  } else if(sum(grepl("Dissatisfied", unlist(x), ignore.case = TRUE)) > 0){
    
    return(case_when(
      x == "Very satisfied" ~ 10,
      x == "Satisfied" ~ 7.5,
      x == "Neither satisfied nor dissatisfied" ~ 5,
      x == "Dissatisfied" ~ 2.5,
      x == "Very dissatisfied" ~ 0,
      TRUE ~ NA_real_
    ))

  } else {
    
    return(x)
  }
})

Glorious. R makes it too easy, really, I think, sometimes 🙂