((45 + 21)^3 + 3/4)/(sqrt(32 - 12/17))
## [1] 51392.72
(sqrt(7) - pi)/(3 * (45 - 34))
## [1] -0.0150255
value <- 3
value %% 2 == 0
## [1] FALSE
value <- 4
value %% 2 == 0
## [1] TRUE
value <- 3
abs(value) >= 2 & abs(value) <= 4
## [1] TRUE
4 ^ 3 %in% c(2,3,4)
and 4 * 3 %in% c(2,3,4)
.
4 ^ 3 %in% c(2,3,4)
## [1] FALSE
4 * 3 %in% c(2,3,4)
## [1] 4
x
with all even numbers between 1 e 25 .
x <- seq(from = 2, to = 25, by = 2)
x
## [1] 2 4 6 8 10 12 14 16 18 20 22 24
y
with the first 10 multiples of 7 starting from 14.
y <- seq(from = 14, by = 7, length.out = 10)
y
## [1] 14 21 28 35 42 49 56 63 70 77
a
with the letters "A"
,"B"
e "C"
repeated in the same order 4 times.
a <- rep(c("A", "B", "C"), times = 4)
a
## [1] "A" "B" "C" "A" "B" "C" "A" "B" "C" "A" "B" "C"
b
with the letters "A"
,"B"
e "C"
each one repeated 4 times.
b <- rep(c("A", "B", "C"), each = 4)
b
## [1] "A" "A" "A" "A" "B" "B" "B" "B" "C" "C" "C" "C"
x
, select the values 8, 12, and 16.
x[c(4, 6, 8)]
## [1] 8 12 16
y
, select values smaller than 36 or grater than 54.
y[y < 36 | y > 54]
## [1] 14 21 28 35 56 63 70 77
c
from a
removing all "B"
elements.
c <- a[a != "B"]
c
## [1] "A" "C" "A" "C" "A" "C" "A" "C"
d
from b
substituting all "B"
with "D"
.
d <- b
d[d == "B"] <- "D"
d
## [1] "A" "A" "A" "A" "D" "D" "D" "D" "C" "C" "C" "C"
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
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
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
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
A
as follows,```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
```
B
4X3 in which "A"
,"B"
e "C"
are repeated at each row.
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"
A
, select the value 27.
A[2, 3]
## [1] 27
B
, select the elements between the second and forth row, second and third column.
B[2:4, c(2,3)]
## [,1] [,2]
## [1,] "B" "C"
## [2,] "B" "C"
## [3,] "B" "C"
A
, select only odd values.
A[A %% 2 == 1]
## [1] 23 93 27 7 7 99
B
, select all values different from "C"
.
B[B != "C"]
## [1] "A" "A" "A" "A" "B" "B" "B" "B"
## 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
```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
```
"subj_3"
.
my_data[my_data$Id == "subj_3", ]
## Id age gender item_1 item_2 item_3
## 3 subj_3 19 F 1 1 1
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
Id
and gender
for subject with 1
at item_1
.
my_data[my_data$item_1 == 1, c("Id", "gender")]
## Id gender
## 2 subj_2 M
## 3 subj_3 F
x
A
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
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
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
"Hello World!"
to the list.
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!"