R Project: Survey Analysis
A survey analysis turns raw responses into a short summary. The steps clean the answers, group them, add outside facts and report the result.
Build and clean the responses
The responses frame starts with town names that use underscores. gsub() replaces each underscore with a space and the cleaned frame prints.
responses <- data.frame(id = c(1, 2, 3, 4), town = c("New_York", "Boston", "New_York", "Boston"), score = c(8, 6, 9, 7), rating = c("good", "poor", "good", "fair"))
responses$town <- gsub("_", " ", responses$town)
print(responses)Put the rating levels in order
factor() builds the rating factor from the rating column. relevel() moves poor to the front, so the level order runs poor, fair and good.
responses <- data.frame(id = c(1, 2, 3, 4), town = c("New_York", "Boston", "New_York", "Boston"), score = c(8, 6, 9, 7), rating = c("good", "poor", "good", "fair"))
rating <- factor(responses$rating)
rating <- relevel(rating, "poor")
print(rating)
print(levels(rating))Mean score for each town
aggregate() reads the formula as score grouped by town. The result holds one row per town with its mean score.
responses <- data.frame(id = c(1, 2, 3, 4), town = c("New_York", "Boston", "New_York", "Boston"), score = c(8, 6, 9, 7), rating = c("good", "poor", "good", "fair"))
responses$town <- gsub("_", " ", responses$town)
averages <- aggregate(score ~ town, data = responses, mean)
print(averages)Add leaders with a merge
The lookup frame pairs each town with one leader. merge() joins on the shared town column and sorts the rows by town.
responses <- data.frame(id = c(1, 2, 3, 4), town = c("New_York", "Boston", "New_York", "Boston"), score = c(8, 6, 9, 7), rating = c("good", "poor", "good", "fair"))
responses$town <- gsub("_", " ", responses$town)
leaders <- data.frame(town = c("Boston", "New York"), leader = c("Ana", "Ben"))
joined <- merge(responses, leaders, by = "town")
print(joined)Example
responses <- data.frame(id = c(1, 2, 3, 4), town = c("New_York", "Boston", "New_York", "Boston"), score = c(8, 6, 9, 7), rating = c("good", "poor", "good", "fair"))
responses$town <- gsub("_", " ", responses$town)
rating <- factor(responses$rating)
rating <- relevel(rating, "poor")
leaders <- data.frame(town = c("Boston", "New York"), leader = c("Ana", "Ben"))
joined <- merge(responses, leaders, by = "town")
means <- tapply(responses$score, responses$town, mean)
print(joined)
print(levels(rating))
cat(sprintf("New York average: %.1f from %d responses\n", means[["New York"]], nrow(responses)))Key points
- The responses frame holds one row per answer, with a town, a score and a rating for each.
- gsub() cleans a text column by replacing one pattern everywhere it appears.
- aggregate() gives one mean score row for each town.
- merge() adds a team leader for each town from a small lookup frame.
- relevel() puts the rating levels in a chosen order that starts with poor.
- sprintf() builds the report line and cat() prints it as plain text.
In this example the full survey runs end to end. The town names are cleaned, the rating levels start with poor, the merge adds a leader per town, and the report line gives the New York mean from tapply() with the response count.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท rdrr.io R Snippets
Exercise
What does the report line at the end of the example show?