Here’s a good example of how useful
sapply can be. I have some data from Qualtrics, and each response is coded in its own variable. Let’s say there is a question on what kind of organization respondents work in, with 10 response categories. Qualtrics produces 10 variables, each with 1 if the box was ticked, and empty otherwise (structure shown just below). With the default CSV import, these blank cells are turned into NA. Here’s a simple way to produce a barplot in this case (in R, of course).
types = sapply(1:10, function(i) sum(get(paste("Q1_",i,sep="")), na.rm=TRUE))
Let’s take this step by step. To count frequencies, we simply use
sum(), with the argument
na.rm=TRUE because the variables only contain 1 and NA.
get() is used to find the variable specified by a string; the string is created with
paste(). In this case, the variable names are Q1_1, Q1_2, Q1_3, … Q1_9, Q1_10. By using
paste(), we combine the “Q1_” part with the counter variable i, with no separation (
The whole thing is then wrapped up in
sapply(), with the counter variable i defined to take values from 1 to 10; the
function(i) part is there so that the counter variable is applied to the sum. So
sapply() takes each value of the counter variable, and applies it to the function we specified, which calculates the sum for one variable Q1_i at a time.
Now I can simply do a boxplot, and add the
names.arg argument to specify the labels.
(Here I specified the colours:
barplot(types, col=rainbow(10)) to have a catchy image at the top of this post, albeit one where colours have no meaning: so-called chart-junk).