Session Ready
Exercise

The if-else structure

The math_por data frame now contains - in addition to the background variables used for joining por and math - two possibly different answers to the same questions for each student. To fix this, you'll use programming to combine these 'duplicated' answers by either:

  • taking the rounded average (if the two variables are numeric)
  • simply choosing the first answer (else).

You'll do this by using a combination of a for-loop and an if-else structure.

The if() function takes a single logical condition as an argument and performs an action only if that condition is true. if can then be combined with else, which handles the cases where the condition is false.

if(condition) {
   do something
} else {
   do something else
}
Instructions
100 XP
  • Please note! Here in DataCamp, executing a command over multiple lines is best done by selecting (painting) the lines with a mouse first and then hitting Ctrl+Enter normally.
  • Print out the column names of math_por
  • Adjust the code: Create the data frame alc by selecting only the columns in math_por which were used for joining the two questionnaires. The names of those columns are available in the join_by object.
  • Create the object notjoined_columns and print it out.
  • Execute the for-loop (don't mind the "change me!").
  • Take a glimpse() at the alc data frame. The factor type variables should look strange at this point.
  • Adjust the code inside the for-loop: if the first of the two selected columns is not numeric, add the first column to the alc data frame.
  • Execute the modified for-loop and glimpse() at the new data again.