Compare matrices
R's ability to deal with different data structures for comparisons does not stop at vectors. Matrices and relational operators also work together seamlessly!
Instead of in vectors (as in the previous exercise), the LinkedIn and Facebook data is now stored in a matrix called views
. The first row contains the LinkedIn information; the second row the Facebook information. The original vectors facebook
and linkedin
are still available as well.
This is a part of the course
“Intermediate R”
Exercise instructions
Using the relational operators you've learned so far, try to discover the following:
- When were the views exactly equal to 13? Use the
views
matrix to return a logical matrix. - For which days were the number of views less than or equal to 14? Again, have R return a logical matrix.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# The social data has been created for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)
views <- matrix(c(linkedin, facebook), nrow = 2, byrow = TRUE)
# When does views equal 13?
# When is views less than or equal to 14?
This exercise is part of the course
Intermediate R
Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.
In this chapter, you'll learn about relational operators for comparing R objects, and logical operators like "and" and "or" for combining TRUE and FALSE values. Then, you'll use this knowledge to build conditional statements.
Exercise 1: Relational OperatorsExercise 2: EqualityExercise 3: Greater and less thanExercise 4: Compare vectorsExercise 5: Compare matricesExercise 6: Logical OperatorsExercise 7: & and |Exercise 8: & and | (2)Exercise 9: Reverse the result: !Exercise 10: Blend it all togetherExercise 11: Conditional StatementsExercise 12: The if statementExercise 13: Add an elseExercise 14: Customize further: else ifExercise 15: Else if 2.0Exercise 16: Take control!What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.