1 Operators

  1. Compute \(\frac{(45+21)^3+\frac{3}{4}}{\sqrt{32-\frac{12}{17}}}\).
    See solution
    ((45 + 21)^3 + 3/4)/(sqrt(32 - 12/17))
    ## [1] 51392.72
  2. Compute \(\frac{\sqrt{7-\pi}}{3\ (45-34)}\).
    See solution
    (sqrt(7) - pi)/(3 * (45 - 34))
    ## [1] -0.0150255
  3. Define a proposition to evaluate if a number is even.
    See solution
    value <- 3
    value %% 2 == 0
    ## [1] FALSE
    
    value <- 4
    value %% 2 == 0
    ## [1] TRUE
  4. Define a proposition to evaluate if a number is between -4 and -2 or between 2 and 4.
    See solution
    value <- 3
    abs(value) >= 2 & abs(value) <= 4
    ## [1] TRUE
  5. Compare the returned results by 4 ^ 3 %in% c(2,3,4) and 4 * 3 %in% c(2,3,4).
    See solution
    4 ^ 3 %in% c(2,3,4)
    ## [1] FALSE
    
    4 * 3 %in% c(2,3,4)
    ## [1] 4

2 Vectors

Creating

  1. Create x with all even numbers between 1 e 25 .
    See solution
    x <- seq(from = 2, to = 25, by = 2)
    x
    ##  [1]  2  4  6  8 10 12 14 16 18 20 22 24
  2. Create y with the first 10 multiples of 7 starting from 14.
    See solution
    y <- seq(from = 14, by = 7, length.out = 10)
    y
    ##  [1] 14 21 28 35 42 49 56 63 70 77
  3. Create a with the letters "A","B" e "C" repeated in the same order 4 times.
    See solution
    a <- rep(c("A", "B", "C"), times = 4)
    a
    ##  [1] "A" "B" "C" "A" "B" "C" "A" "B" "C" "A" "B" "C"
  4. Create b with the letters "A","B" e "C" each one repeated 4 times.
    See solution
    b <- rep(c("A", "B", "C"), each = 4)
    b
    ##  [1] "A" "A" "A" "A" "B" "B" "B" "B" "C" "C" "C" "C"

Selecting

  1. From x, select the values 8, 12, and 16.
    See solution
    x[c(4, 6, 8)]
    ## [1]  8 12 16
  2. From y, select values smaller than 36 or grater than 54.
    See solution
    y[y < 36 | y > 54]
    ## [1] 14 21 28 35 56 63 70 77
  3. Create c from a removing all "B" elements.
    See solution
    c <- a[a != "B"]
    c
    ## [1] "A" "C" "A" "C" "A" "C" "A" "C"
  4. Crate d from b substituting all "B" with "D".
    See solution
    d <- b
    d[d == "B"] <- "D"
    d
    ##  [1] "A" "A" "A" "A" "D" "D" "D" "D" "C" "C" "C" "C"

3 Factors

  1. Create the factor gender as follows,

    ## [1] M F M F M F F F M
    ## Levels: F M

    See solution

    gender <- factor(c("M", "F", "M", "F", "M", "F", "F", "F", "M"))
    gender
    ## [1] M F M F M F F F M
    ## Levels: F M
  2. Rename levels in "Males" and "Females"

    See solution

    levels(gender) <- c("Females", "Males")
    gender
    ## [1] Males   Females Males   Females Males   Females Females Females Males  
    ## Levels: Females Males
  3. Create the factor size as follows,

    c("Small", "Large", "Medium", "X-Large", "X-Small")

    See solution

    size <- factor(c("Small", "Large", "Medium", "X-Large", "X-Small"))
    size
    ## [1] Small   Large   Medium  X-Large X-Small
    ## Levels: Large Medium Small X-Large X-Small
  4. Make size an ordered factor, with appropriate label order.

    See solution

    size <- factor(c("Small", "Large", "Medium", "X-Large", "X-Small"),
                   levels = c("X-Small", "Small", "Medium", "Large", "X-Large"),
                   ordered = TRUE)
    size
    ## [1] Small   Large   Medium  X-Large X-Small
    ## Levels: X-Small < Small < Medium < Large < X-Large

4 Matrices

Creating

  1. Create the matrix A as follows,
\[ \begin{matrix} 2 & 34 & 12 & 7\\ 46 & 93 & 27 & 99\\ 23 & 38 & 7 & 04 \end{matrix} \]
See solution
```r
A <- matrix(c(2, 34, 12, 7, 46, 93, 27, 99, 23, 38, 7, 04),
            ncol = 4, byrow = TRUE)
A
##      [,1] [,2] [,3] [,4]
## [1,]    2   34   12    7
## [2,]   46   93   27   99
## [3,]   23   38    7    4
```
  1. Create the matrix B4X3 in which "A","B" e "C" are repeated at each row.
    See solution
    B <-  matrix(c("A", "B", "C"), ncol = 3, nrow = 4, byrow = TRUE)
    B
    ##      [,1] [,2] [,3]
    ## [1,] "A"  "B"  "C" 
    ## [2,] "A"  "B"  "C" 
    ## [3,] "A"  "B"  "C" 
    ## [4,] "A"  "B"  "C"

Selecting

  1. From A, select the value 27.
    See solution
    A[2, 3]
    ## [1] 27
  2. From B, select the elements between the second and forth row, second and third column.
    See solution
    B[2:4, c(2,3)]
    ##      [,1] [,2]
    ## [1,] "B"  "C" 
    ## [2,] "B"  "C" 
    ## [3,] "B"  "C"
  3. Form A, select only odd values.
    See solution
    A[A %% 2 == 1]
    ## [1] 23 93 27  7  7 99
  4. From B, select all values different from "C".
    See solution
    B[B != "C"]
    ## [1] "A" "A" "A" "A" "B" "B" "B" "B"

5 Dataframe

Creating

  1. Create the following dataframe
##       Id age gender item_1 item_2 item_3
## 1 subj_1  21      F      2      0      2
## 2 subj_2  23      M      1      2      0
## 3 subj_3  19      F      1      1      1
See solution
```r
my_data <- data.frame(Id = c("subj_1","subj_2","subj_3"),
                      age = c(21,23,19),
                      gender = c("F","M","F"),
                      item_1 = c(2,1,1),
                      item_2 = c(0,2,1),
                      item_3 = c(2,0,1))
my_data
##       Id age gender item_1 item_2 item_3
## 1 subj_1  21      F      2      0      2
## 2 subj_2  23      M      1      2      0
## 3 subj_3  19      F      1      1      1
```

Selecting

  1. Select data of "subj_3".
    See solution
    my_data[my_data$Id == "subj_3", ]
    ##       Id age gender item_1 item_2 item_3
    ## 3 subj_3  19      F      1      1      1
  2. Select only the answers to the items.
    See solution
    my_data[, c("item_1", "item_2", "item_3")]
    ##   item_1 item_2 item_3
    ## 1      2      0      2
    ## 2      1      2      0
    ## 3      1      1      1
  3. Select Id and gender for subject with 1 at item_1.
    See solution
    my_data[my_data$item_1 == 1, c("Id", "gender")]
    ##       Id gender
    ## 2 subj_2      M
    ## 3 subj_3      F

6 Lists

Creating

  1. Create a list with the elements form the previous exercises:
  • vector x
  • matrix A
  • dataframe
    See solution
    my_list <- list(x = x,
                    A = A,
                    my_data = my_data)
    my_list
    ## $x
    ##  [1]  2  4  6  8 10 12 14 16 18 20 22 24
    ## 
    ## $A
    ##      [,1] [,2] [,3] [,4]
    ## [1,]    2   34   12    7
    ## [2,]   46   93   27   99
    ## [3,]   23   38    7    4
    ## 
    ## $my_data
    ##       Id age gender item_1 item_2 item_3
    ## 1 subj_1  21      F      2      0      2
    ## 2 subj_2  23      M      1      2      0
    ## 3 subj_3  19      F      1      1      1

Selecting

  1. Select the vector and the matrix from the list.
    See solution
    my_list[1:2]
    ## $x
    ##  [1]  2  4  6  8 10 12 14 16 18 20 22 24
    ## 
    ## $A
    ##      [,1] [,2] [,3] [,4]
    ## [1,]    2   34   12    7
    ## [2,]   46   93   27   99
    ## [3,]   23   38    7    4
  2. Extract the dataframe from the list.
    See solution
    my_list[["my_data"]]
    ##       Id age gender item_1 item_2 item_3
    ## 1 subj_1  21      F      2      0      2
    ## 2 subj_2  23      M      1      2      0
    ## 3 subj_3  19      F      1      1      1
  3. Add the sting "Hello World!" to the list.
    See solution
    my_list$string <- "Hello World!"
    my_list
    ## $x
    ##  [1]  2  4  6  8 10 12 14 16 18 20 22 24
    ## 
    ## $A
    ##      [,1] [,2] [,3] [,4]
    ## [1,]    2   34   12    7
    ## [2,]   46   93   27   99
    ## [3,]   23   38    7    4
    ## 
    ## $my_data
    ##       Id age gender item_1 item_2 item_3
    ## 1 subj_1  21      F      2      0      2
    ## 2 subj_2  23      M      1      2      0
    ## 3 subj_3  19      F      1      1      1
    ## 
    ## $string
    ## [1] "Hello World!"