Using operators and mean() to summarize Likert data

I remain an R newbie, so when I saw this tweet from Simon Jackson it kinda blew my mind. You can use logical operators with the mean() command to check the proportion of a vector that meets the criteria described by the operators. For example, you can look at what proportion of of a group of numbers is above, say, 900:

sample <- c(1234,1600,900,856,NA,1)
mean(sample > 900, na.rm = TRUE)

## [1] 0.4

This is particularly handy for summarizing Likert-type data. For example, I often have to check what percentage of respondents agreed with a statement. This would normally require adding all of those who agreed + those who strongly agreed with the statement and dividing by the number of respondents. The mean() + operators technique makes the process a bit easier.

First, let’s create fake Likert-type data, assuming a 5-point scale from strongly disagree to strongly agree. For illustration, I’ll make 40% of the 20 responses either “somewhat agree” (4) or “strongly agree” (5) and 35% “somewhat disagree” (2) or “strongly disagree” (1):

likertData <- c(1,1,5,4,5,3,3,2,NA,1,2,5,5,4,3,3,4,2,NA,3,1,5)

All of those who chose 4 or 5 agree with a statment. Find the proportion using mean() and operators:

mean(likertData > 3, na.rm = TRUE)

## [1] 0.40

mean(likertData < 3, na.rm = TRUE)

## [1] 0.35

Maybe you want to see how many people gave extreme answers:

mean(likertData == 1 | likertData == 5, na.rm = TRUE)

## [1] 0.45

And so on.

Thanks to Simon for pointing that out. Of course, had I bothered to RTFM, I’d have seen that “Currently there are methods for numeric/logical vectors and date, date-time and time interval objects” right at the top