2.5 Exercises

i2ds: Exercises

2.5.1 The ER of R

When discussing the fit between tools and tasks in Chapter 1, we encountered the notion of ecological rationality (ER, in Section 1.2.6). Now that we have learned some basic R concepts and commands, we can ask:

  • Where would R be in Figure 1.7?

  • Where would we place an individual R function?

  • How does the arrangement change when addressing R (the language), RStudio (the software), or some R package?

Please note that thinking too long about this may make us quite dizzy.

2.5.2 Data types and forms

Please answer the following questions in a few sentences:

  1. Describe the similarities and differences between scalars and vectors in R.

  2. Describe the difference between logical and numeric indexing in your own words.

2.5.3 Exploring a function

2.5.4 Cumulative savings

2.5.5 Vector arithmetic

2.5.6 Survey age

2.5.7 Cryptic arithmetic

2.5.8 Manipulating matrices

Assuming a matrix mx:

mx <- matrix(letters[1:4], nrow = 2, ncol = 2, byrow = TRUE)
#>      [,1] [,2]
#> [1,] "a"  "b" 
#> [2,] "c"  "d"

Write R expressions that either apply functions or use some form of indexing to retrieve and replace individual elements for creating the following variants of the matrix mx:

# (a)
mx_1  # transpose mx: 
#>      [,1] [,2]
#> [1,] "a"  "c" 
#> [2,] "b"  "d"

# (b)
mx_2  # mirror/swap rows of mx: 
#>      [,1] [,2]
#> [1,] "c"  "d" 
#> [2,] "a"  "b"

# (c)
mx_3  # mirror/swap columns of mx: 
#>      [,1] [,2]
#> [1,] "b"  "a" 
#> [2,] "d"  "c"

# (d)
mx_4  # swap only the elements of the 2nd column of mx: 
#>      [,1] [,2]
#> [1,] "a"  "d" 
#> [2,] "b"  "b"

Hint: This exercise could trivially be solved by creating the matrices mx_1 to mx_4 from scratch. However, the purpose of the exercise is to use indexing for retrieving and replacing matrix elements.

2.5.9 Exploring participant data

This concludes our first set of exercises on basic R concepts and commands.