Get startedGet started for free

Combining variables

Our data includes many questions that can be thought to measure the same dimension. You can read more about the data and the variables here. Here we'll combine multiple questions into combination variables. Useful functions for summation with data frames in R are

function description
colSums(df) returns a sum of each column in df
rowSums(df) returns a sum of each row in df
colMeans(df) returns the mean of each column in df
rowMeans(df) return the mean of each row in df

We'll combine the use of rowMeans()with the select() function from the dplyr library to average the answers of selected questions. See how it is done from the example codes.

This exercise is part of the course

Helsinki Open Data Science

View Course

Exercise instructions

  • Access the dplyr library
  • Execute the example codes to create the combination variables 'deep' and 'surf' as columns in lrn14
  • Select the columns related to strategic learning from lrn14
  • Create the combination variable 'stra' as a column in lrn14

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# lrn14 is available

# Access the dplyr library
library(dplyr)

# questions related to deep, surface and strategic learning
deep_questions <- c("D03", "D11", "D19", "D27", "D07", "D14", "D22", "D30","D06",  "D15", "D23", "D31")
surface_questions <- c("SU02","SU10","SU18","SU26", "SU05","SU13","SU21","SU29","SU08","SU16","SU24","SU32")
strategic_questions <- c("ST01","ST09","ST17","ST25","ST04","ST12","ST20","ST28")

# select the columns related to deep learning and create column 'deep' by averaging
deep_columns <- select(lrn14, one_of(deep_questions))
lrn14$deep <- rowMeans(deep_columns)

# select the columns related to surface learning and create column 'surf' by averaging
surface_columns <- select(lrn14, one_of(surface_questions))
lrn14$surf <- rowMeans(surface_columns)

# select the columns related to strategic learning and create column 'stra' by averaging
strategic_columns <- "change me!"

Edit and Run Code