a <- 2.3
result <- (6 * a + 42) / (3^(4.2-3.62))
result
## [1] 29.50556
(-4)^2+2 # this is the one that squares negative 4and adds 2 to the result
## [1] 18
G<-c(25.2,15,16.44,15.3,18.6)
Average<-mean(G)
Average
## [1] 18.108
Half_avg<-Average/2
Half_avg
## [1] 9.054
sqrt(Half_avg)
## [1] 3.008987
log(x=0.3)
## [1] -1.203973
exp(x=-1.203973)
## [1] 0.2999999
-0.00000000423546322
## [1] -4.235463e-09
a<-3^(2)*4^(1/8)
a
## [1] 10.70286
a<-a/2.33
a
## [1] 4.593504
c<--8.2*10^-13
c
## [1] -8.2e-13
d<-a*c
d
## [1] -3.766673e-12
S <- seq(5, -11, by = -0.3)
S
## [1] 5.0 4.7 4.4 4.1 3.8 3.5 3.2 2.9 2.6 2.3 2.0 1.7
## [13] 1.4 1.1 0.8 0.5 0.2 -0.1 -0.4 -0.7 -1.0 -1.3 -1.6 -1.9
## [25] -2.2 -2.5 -2.8 -3.1 -3.4 -3.7 -4.0 -4.3 -4.6 -4.9 -5.2 -5.5
## [37] -5.8 -6.1 -6.4 -6.7 -7.0 -7.3 -7.6 -7.9 -8.2 -8.5 -8.8 -9.1
## [49] -9.4 -9.7 -10.0 -10.3 -10.6 -10.9
S<-rev(S)
S
## [1] -10.9 -10.6 -10.3 -10.0 -9.7 -9.4 -9.1 -8.8 -8.5 -8.2 -7.9 -7.6
## [13] -7.3 -7.0 -6.7 -6.4 -6.1 -5.8 -5.5 -5.2 -4.9 -4.6 -4.3 -4.0
## [25] -3.7 -3.4 -3.1 -2.8 -2.5 -2.2 -1.9 -1.6 -1.3 -1.0 -0.7 -0.4
## [37] -0.1 0.2 0.5 0.8 1.1 1.4 1.7 2.0 2.3 2.6 2.9 3.2
## [49] 3.5 3.8 4.1 4.4 4.7 5.0
R<-rep(x=c(-1,3,-5,7,-9),times=2,each=10)
R
## [1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3 3 3 -5 -5 -5 -5 -5
## [26] -5 -5 -5 -5 -5 7 7 7 7 7 7 7 7 7 7 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
## [51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3 3 3 -5 -5 -5 -5 -5
## [76] -5 -5 -5 -5 -5 7 7 7 7 7 7 7 7 7 7 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
sort(x=R,decreasing=TRUE)
## [1] 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 3 3 3 3 3
## [26] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
## [51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
## [76] -5 -5 -5 -5 -5 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
length(x=R)
## [1] 100
sequence_1 <- 6:12
sequence_1
## [1] 6 7 8 9 10 11 12
sequence_2 <- rep(5.3, times = 3)
sequence_2
## [1] 5.3 5.3 5.3
sequence_3 <- -3
sequence_3
## [1] -3
combined_vector <- c(sequence_1, sequence_2, sequence_3)
combined_vector
## [1] 6.0 7.0 8.0 9.0 10.0 11.0 12.0 5.3 5.3 5.3 -3.0
length_of_combined <- length(combined_vector)
length_of_combined
## [1] 11
sequence_4 <- seq(from = 102, to = length_of_combined, length.out = 9)
final_vector <- c(sequence_1, sequence_2, sequence_3, sequence_4)
final_vector
## [1] 6.000 7.000 8.000 9.000 10.000 11.000 12.000 5.300 5.300
## [10] 5.300 -3.000 102.000 90.625 79.250 67.875 56.500 45.125 33.750
## [19] 22.375 11.000
length(final_vector)
## [1] 20
seq_part <- 3:6
length(seq_part)
## [1] 4
seq_part
## [1] 3 4 5 6
repeat_part <- rep(c(2, -5.1, -33), 2)
value_part <- (7 / 42) + 2
vector_a <- c(seq_part, repeat_part, value_part)
print(vector_a)
## [1] 3.000000 4.000000 5.000000 6.000000 2.000000 -5.100000
## [7] -33.000000 2.000000 -5.100000 -33.000000 2.166667
first_last <- c(vector_a[1], vector_a[length(vector_a)])
print(first_last)
## [1] 3.000000 2.166667
omit_first_last <- vector_a[2:(length(vector_a) - 1)]
print(omit_first_last)
## [1] 4.0 5.0 6.0 2.0 -5.1 -33.0 2.0 -5.1 -33.0
reconstructed_a <- c(first_last[1], omit_first_last, first_last[2])
print(reconstructed_a)
## [1] 3.000000 4.000000 5.000000 6.000000 2.000000 -5.100000
## [7] -33.000000 2.000000 -5.100000 -33.000000 2.166667
vector_a <- sort(vector_a)
print(vector_a)
## [1] -33.000000 -33.000000 -5.100000 -5.100000 2.000000 2.000000
## [7] 2.166667 3.000000 4.000000 5.000000 6.000000
reverse_order <- vector_a[length(vector_a):1]
sorted_decreasing <- sort(vector_a, decreasing = TRUE)
print(reverse_order)
## [1] 6.000000 5.000000 4.000000 3.000000 2.166667 2.000000
## [7] 2.000000 -5.100000 -5.100000 -33.000000 -33.000000
print(sorted_decreasing)
## [1] 6.000000 5.000000 4.000000 3.000000 2.166667 2.000000
## [7] 2.000000 -5.100000 -5.100000 -33.000000 -33.000000
repeated_vector <- c(rep(omit_first_last[3], 3),
rep(omit_first_last[6], 4),
omit_first_last[length(omit_first_last)])
print(repeated_vector)
## [1] 6 6 6 -33 -33 -33 -33 -33
vec_a <- c(2, 0.5, 1, 2, 0.5, 1, 2, 0.5, 1)
ones_vector <- rep(1, length(vec_a))
print(ones_vector)
## [1] 1 1 1 1 1 1 1 1 1
fahrenheit_values <- c(45, 77, 20, 19, 101, 120, 212)
celsius_values <- (5 / 9) * (fahrenheit_values - 32)
print(celsius_values)
## [1] 7.222222 25.000000 -6.666667 -7.222222 38.333333 48.888889 100.000000
vec_c <- rep(c(2, 4, 6), each = 2) * c(1, 2)
print(vec_c)
## [1] 2 4 4 8 6 12
vec_c[2:5] <- c(-0.1, -100)
print(vec_c)
## [1] 2.0 -0.1 -100.0 -0.1 -100.0 12.0
matrix_a <- matrix(c(4.3, 3.1, 8.2, 8.2, 3.2, 0.9, 1.6, 6.5), nrow = 4, ncol = 2, byrow = TRUE)
matrix_a
## [,1] [,2]
## [1,] 4.3 3.1
## [2,] 8.2 8.2
## [3,] 3.2 0.9
## [4,] 1.6 6.5
matrix_b <- matrix_a[-1, ]
dim(matrix_b)
## [1] 3 2
matrix_a[, 2] <- sort(matrix_a[, 2])
matrix_a
## [,1] [,2]
## [1,] 4.3 0.9
## [2,] 8.2 3.1
## [3,] 3.2 6.5
## [4,] 1.6 8.2
matrix_d <- matrix_a[-4, -1]
matrix_d <- matrix(matrix_d)
matrix_d
## [,1]
## [1,] 0.9
## [2,] 3.1
## [3,] 6.5
matrix_e <- matrix(matrix_a[3:4, ], nrow = 2, ncol = 2)
matrix_e
## [,1] [,2]
## [1,] 3.2 6.5
## [2,] 1.6 8.2
matrix_a[c(4,1),c(2,1)] <- -0.5*diag(matrix_e)
matrix_a
## [,1] [,2]
## [1,] -4.1 -4.1
## [2,] 8.2 3.1
## [3,] 3.2 6.5
## [4,] -1.6 -1.6
matrix_a[c(4,1),c(1,1)] <- -0.5*diag(matrix_e)
matrix1 <- matrix(c(1, 2, 2, 4, 7, 6), nrow=3, ncol=2, byrow=TRUE)
matrix2 <- matrix(c(10, 20, 30, 40, 50, 60), nrow=3, ncol=2, byrow=TRUE)
result <- (2/7) * (matrix1 - matrix2)
result
## [,1] [,2]
## [1,] -2.571429 -5.142857
## [2,] -8.000000 -10.285714
## [3,] -12.285714 -15.428571
A <- matrix(c(1, 2, 7), nrow=3, ncol=1)
B <- matrix(c(3, 4, 8), nrow=3, ncol=1)
t(A)
## [,1] [,2] [,3]
## [1,] 1 2 7
result2 <- t(A) %*% B
result2
## [,1]
## [1,] 67
t(B)
## [,1] [,2] [,3]
## [1,] 3 4 8
result3 <- t(B) %*% (A %*% t(A))
result3
## [,1] [,2] [,3]
## [1,] 67 134 469
A
## [,1]
## [1,] 1
## [2,] 2
## [3,] 7
t(A)
## [,1] [,2] [,3]
## [1,] 1 2 7
t(B)
## [,1] [,2] [,3]
## [1,] 3 4 8
result4 <- (A %*% t(A)) %*% B
result4
## [,1]
## [1,] 67
## [2,] 134
## [3,] 469
I3 <- diag(3)
result5 <- solve((B %*% t(B)) + (A %*% t(A)) - 100 * I3)
result5
## [,1] [,2] [,3]
## [1,] -0.007923676 0.003123274 0.007843334
## [2,] 0.003123274 -0.005350239 0.011483806
## [3,] 0.007843334 0.011483806 0.017584735
A <- diag(c(2, 3, 5, -1))
A_inv <- solve(A)
I4 <- diag(4)
result_c <- A_inv %*% A - I4
result_c
## [,1] [,2] [,3] [,4]
## [1,] 0 0 0 0
## [2,] 0 0 0 0
## [3,] 0 0 0 0
## [4,] 0 0 0 0
seq_vals <- seq(4.8, 0.1, length.out = 48)
arr_a <- array(seq_vals, dim = c(4, 2, 6))
print(arr_a)
## , , 1
##
## [,1] [,2]
## [1,] 4.8 4.4
## [2,] 4.7 4.3
## [3,] 4.6 4.2
## [4,] 4.5 4.1
##
## , , 2
##
## [,1] [,2]
## [1,] 4.0 3.6
## [2,] 3.9 3.5
## [3,] 3.8 3.4
## [4,] 3.7 3.3
##
## , , 3
##
## [,1] [,2]
## [1,] 3.2 2.8
## [2,] 3.1 2.7
## [3,] 3.0 2.6
## [4,] 2.9 2.5
##
## , , 4
##
## [,1] [,2]
## [1,] 2.4 2.0
## [2,] 2.3 1.9
## [3,] 2.2 1.8
## [4,] 2.1 1.7
##
## , , 5
##
## [,1] [,2]
## [1,] 1.6 1.2
## [2,] 1.5 1.1
## [3,] 1.4 1.0
## [4,] 1.3 0.9
##
## , , 6
##
## [,1] [,2]
## [1,] 0.8 0.4
## [2,] 0.7 0.3
## [3,] 0.6 0.2
## [4,] 0.5 0.1
arr_b <- arr_a[c(4, 1), 2, ]
print(arr_b)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 4.1 3.3 2.5 1.7 0.9 0.1
## [2,] 4.4 3.6 2.8 2.0 1.2 0.4
row_b2 <- arr_b[2, ]
rep_row_b2 <- rep(row_b2, 4)
arr_c <- array(rep_row_b2, dim = c(2, 2, 2, 3))
print(arr_c)
## , , 1, 1
##
## [,1] [,2]
## [1,] 4.4 2.8
## [2,] 3.6 2.0
##
## , , 2, 1
##
## [,1] [,2]
## [1,] 1.2 4.4
## [2,] 0.4 3.6
##
## , , 1, 2
##
## [,1] [,2]
## [1,] 2.8 1.2
## [2,] 2.0 0.4
##
## , , 2, 2
##
## [,1] [,2]
## [1,] 4.4 2.8
## [2,] 3.6 2.0
##
## , , 1, 3
##
## [,1] [,2]
## [1,] 1.2 4.4
## [2,] 0.4 3.6
##
## , , 2, 3
##
## [,1] [,2]
## [1,] 2.8 1.2
## [2,] 2.0 0.4
arr_d <- arr_a[, , -6]
print(arr_d)
## , , 1
##
## [,1] [,2]
## [1,] 4.8 4.4
## [2,] 4.7 4.3
## [3,] 4.6 4.2
## [4,] 4.5 4.1
##
## , , 2
##
## [,1] [,2]
## [1,] 4.0 3.6
## [2,] 3.9 3.5
## [3,] 3.8 3.4
## [4,] 3.7 3.3
##
## , , 3
##
## [,1] [,2]
## [1,] 3.2 2.8
## [2,] 3.1 2.7
## [3,] 3.0 2.6
## [4,] 2.9 2.5
##
## , , 4
##
## [,1] [,2]
## [1,] 2.4 2.0
## [2,] 2.3 1.9
## [3,] 2.2 1.8
## [4,] 2.1 1.7
##
## , , 5
##
## [,1] [,2]
## [1,] 1.6 1.2
## [2,] 1.5 1.1
## [3,] 1.4 1.0
## [4,] 1.3 0.9
arr_d[c(2, 4), 2, c(1, 3, 5)] <- -99
print(arr_d)
## , , 1
##
## [,1] [,2]
## [1,] 4.8 4.4
## [2,] 4.7 -99.0
## [3,] 4.6 4.2
## [4,] 4.5 -99.0
##
## , , 2
##
## [,1] [,2]
## [1,] 4.0 3.6
## [2,] 3.9 3.5
## [3,] 3.8 3.4
## [4,] 3.7 3.3
##
## , , 3
##
## [,1] [,2]
## [1,] 3.2 2.8
## [2,] 3.1 -99.0
## [3,] 3.0 2.6
## [4,] 2.9 -99.0
##
## , , 4
##
## [,1] [,2]
## [1,] 2.4 2.0
## [2,] 2.3 1.9
## [3,] 2.2 1.8
## [4,] 2.1 1.7
##
## , , 5
##
## [,1] [,2]
## [1,] 1.6 1.2
## [2,] 1.5 -99.0
## [3,] 1.4 1.0
## [4,] 1.3 -99.0
vec_a <- c(6, 9, 7, 3, 6, 7, 9, 6, 3, 6, 6, 7, 1, 9, 1)
vec_a[vec_a == 6]
## [1] 6 6 6 6 6
vec_a[vec_a >= 6]
## [1] 6 9 7 6 7 9 6 6 6 7 9
vec_a[vec_a < 8]
## [1] 6 7 3 6 7 6 3 6 6 7 1 1
vec_a[vec_a != 6]
## [1] 9 7 3 7 9 3 7 1 9 1
vec_b <- vec_a[4:15]
arr_b <- array(vec_b, dim = c(2, 2, 3))
arr_b[arr_b <= (6/2 + 4)]
## [1] 3 6 7 6 3 6 6 7 1 1
arr_b_plus2 <- arr_b + 2
arr_b_plus2[arr_b_plus2 <= (6/2 + 4)]
## [1] 5 5 3 3
I10 <- diag(1, 10)
which(I10 == 0, arr.ind = TRUE)
## row col
## [1,] 2 1
## [2,] 3 1
## [3,] 4 1
## [4,] 5 1
## [5,] 6 1
## [6,] 7 1
## [7,] 8 1
## [8,] 9 1
## [9,] 10 1
## [10,] 1 2
## [11,] 3 2
## [12,] 4 2
## [13,] 5 2
## [14,] 6 2
## [15,] 7 2
## [16,] 8 2
## [17,] 9 2
## [18,] 10 2
## [19,] 1 3
## [20,] 2 3
## [21,] 4 3
## [22,] 5 3
## [23,] 6 3
## [24,] 7 3
## [25,] 8 3
## [26,] 9 3
## [27,] 10 3
## [28,] 1 4
## [29,] 2 4
## [30,] 3 4
## [31,] 5 4
## [32,] 6 4
## [33,] 7 4
## [34,] 8 4
## [35,] 9 4
## [36,] 10 4
## [37,] 1 5
## [38,] 2 5
## [39,] 3 5
## [40,] 4 5
## [41,] 6 5
## [42,] 7 5
## [43,] 8 5
## [44,] 9 5
## [45,] 10 5
## [46,] 1 6
## [47,] 2 6
## [48,] 3 6
## [49,] 4 6
## [50,] 5 6
## [51,] 7 6
## [52,] 8 6
## [53,] 9 6
## [54,] 10 6
## [55,] 1 7
## [56,] 2 7
## [57,] 3 7
## [58,] 4 7
## [59,] 5 7
## [60,] 6 7
## [61,] 8 7
## [62,] 9 7
## [63,] 10 7
## [64,] 1 8
## [65,] 2 8
## [66,] 3 8
## [67,] 4 8
## [68,] 5 8
## [69,] 6 8
## [70,] 7 8
## [71,] 9 8
## [72,] 10 8
## [73,] 1 9
## [74,] 2 9
## [75,] 3 9
## [76,] 4 9
## [77,] 5 9
## [78,] 6 9
## [79,] 7 9
## [80,] 8 9
## [81,] 10 9
## [82,] 1 10
## [83,] 2 10
## [84,] 3 10
## [85,] 4 10
## [86,] 5 10
## [87,] 6 10
## [88,] 7 10
## [89,] 8 10
## [90,] 9 10
logic_b1 <- arr_b <= (6/2 + 4)
any(logic_b1)
## [1] TRUE
all(logic_b1)
## [1] FALSE
logic_b2 <- arr_b_plus2 <= (6/2 + 4)
any(logic_b2)
## [1] TRUE
all(logic_b2)
## [1] FALSE
logic_c <- I10 == 0
diag_c <- diag(logic_c)
any(diag_c)
## [1] FALSE
foo <- c(7, 5, 6, 1, 2, 10, 8, 3, 8, 2)
bar <- foo[foo >= 5]
print(bar)
## [1] 7 5 6 10 8 8
remaining <- foo[foo < 5]
print(remaining)
## [1] 1 2 3 2
baz <- matrix(bar, nrow = 2, byrow = TRUE)
print(baz)
## [,1] [,2] [,3]
## [1,] 7 5 6
## [2,] 10 8 8
baz[baz == 8] <- baz[1, 2]^2
print(baz)
## [,1] [,2] [,3]
## [1,] 7 5 6
## [2,] 10 25 25
all(baz <= 25 & baz > 4)
## [1] TRUE
qux <- array(c(10, 5, 1, 4, 7, 4, 3, 3, 1, 3, 4, 3, 1, 7, 8, 3, 7, 3), dim = c(3, 2, 3))
print(qux)
## , , 1
##
## [,1] [,2]
## [1,] 10 4
## [2,] 5 7
## [3,] 1 4
##
## , , 2
##
## [,1] [,2]
## [1,] 3 3
## [2,] 3 4
## [3,] 1 3
##
## , , 3
##
## [,1] [,2]
## [1,] 1 3
## [2,] 7 7
## [3,] 8 3
indices <- which(qux == 3 | qux == 4, arr.ind = TRUE)
print(indices)
## dim1 dim2 dim3
## [1,] 1 2 1
## [2,] 3 2 1
## [3,] 1 1 2
## [4,] 2 1 2
## [5,] 1 2 2
## [6,] 2 2 2
## [7,] 3 2 2
## [8,] 1 2 3
## [9,] 3 2 3
qux[qux < 3 | qux >= 7] <- 100
print(qux)
## , , 1
##
## [,1] [,2]
## [1,] 100 4
## [2,] 5 100
## [3,] 100 4
##
## , , 2
##
## [,1] [,2]
## [1,] 3 3
## [2,] 3 4
## [3,] 100 3
##
## , , 3
##
## [,1] [,2]
## [1,] 100 3
## [2,] 100 100
## [3,] 100 3
num_seq <- seq(-4, 4, length.out = 20)
logical_matrix <- matrix(c(T, T, T, F, T, F, T, F, F), nrow = 3, byrow = FALSE)
char_vector <- c("don", "quixote")
factor_vector <- factor(c("LOW", "MED", "LOW", "MED", "MED", "HIGH"))
logical_matrix[c(2, 1), c(2, 3)]
## [,1] [,2]
## [1,] TRUE FALSE
## [2,] FALSE TRUE
char_vector[char_vector == "quixote"] <- "Quixote"
char_vector[char_vector == "don"] <- "Don"
cat("Windmills! ATTACK!\n - Don Quixote -\n")
## Windmills! ATTACK!
## - Don Quixote -
num_seq[num_seq > 1]
## [1] 1.052632 1.473684 1.894737 2.315789 2.736842 3.157895 3.578947 4.000000
which(factor_vector == "MED")
## [1] 2 4 5
new_list <- list(
facs = factor_vector,
nums = c(3, 2, 1, 3, 3, 4, 5, 4, 9),
nested = list(
first_three = list(num_seq[1:3], logical_matrix[1:3])
)
)
new_list$facs[new_list$nums >= 3]
## [1] LOW MED MED HIGH <NA> <NA> <NA>
## Levels: HIGH LOW MED
new_list$flags <- rep(logical_matrix[, 3], times = 2)
new_list$nums[new_list$flags == FALSE]
## [1] 2 1 3 4 4 9
new_list$nested[[1]][[1]] <- "Don Quixote"
dfane <- data.frame(
person = c("Stan", "Francine", "Steve", "Roger", "Hayley", "Klaus"),
sex = factor(c("M", "F", "M", "M", "F", "M")),
funny = factor(c("High", "Med", "Low", "High", "Med", "Med"), levels = c("Low", "Med", "High"))
)
dfane$age <- c(41, 41, 15, 1600, 21, 60)
dfane <- dfane[, c("person", "age", "sex", "funny")]
mydata <- dfane
mydata2 <- mydata
combined_data <- dfane
subset(dfane, sex == "F" & funny %in% c("Med", "High"))
## person age sex funny
## 2 Francine 41 F Med
## 5 Hayley 21 F Med
subset(dfane, grepl("^S", person))
## person age sex funny
## 1 Stan 41 M High
## 3 Steve 15 M Low
foo <- c(13563, -14156, -14319, 16981, 12921, 11979, 9568, 8833, -12968, 8133)
foo_non_infinite <- foo[!is.infinite(foo^75)]
foo_non_infinite
## [1] 11979 9568 8833 8133
foo_non_negative_infinity <- foo[!is.infinite(foo^75) & foo^75 > 0]
foo_non_negative_infinity
## [1] 11979 9568 8833 8133
bar <- matrix(c(77875.40, 97551.45, 23764.30, -367478.88,
-35466.25, -73338.85, 36599.69, -70585.69,
-39083.81, 55976.34, 76694.82, 47032.00), nrow = 3, byrow = TRUE)
bar_nan_indexes <- which(is.nan(bar^65 / Inf), arr.ind = TRUE)
bar_nan_indexes
## row col
## [1,] 1 1
## [2,] 1 2
## [3,] 2 2
## [4,] 3 2
## [5,] 3 3
## [6,] 1 4
## [7,] 2 4
bar_non_nan_67 <- bar[!is.nan(bar^67 + Inf)]
bar_non_nan_67
## [1] 77875.40 -35466.25 -39083.81 97551.45 55976.34 23764.30 36599.69
## [8] 76694.82 47032.00
bar_negative_infinity_or_finite <- bar[is.finite(bar^67) | is.infinite(bar^67)]
bar_negative_infinity_or_finite
## [1] 77875.40 -35466.25 -39083.81 97551.45 -73338.85 55976.34
## [7] 23764.30 36599.69 76694.82 -367478.88 -70585.69 47032.00
foo <- c(4.3, 2.2, NULL, 2.4, NaN, 3.3, 3.1, NULL, 3.4, NA)
length(foo)
## [1] 8
which(is.na(foo))
## [1] 4 8
is.null(foo)
## [1] FALSE
is.na(foo[8]) + NULL
## integer(0)
alpha <- list(c(7, 7, NA, 3, NA, 1, 1, 5, NA))
is.null(alpha$beta)
## [1] TRUE
alpha$beta <- which(is.na(alpha[[1]]))
alpha
## [[1]]
## [1] 7 7 NA 3 NA 1 1 5 NA
##
## $beta
## [1] 3 5 9
foo <- array(data = 1:36, dim = c(3,3,4))
class(foo)
## [1] "array"
bar <- as.vector(foo)
class(bar)
## [1] "integer"
baz <- as.character(bar)
class(baz)
## [1] "character"
qux <- as.factor(baz)
class(qux)
## [1] "factor"
quux <- barplot(-c(0,1,0,1,0))

class(quux)
## [1] "matrix" "array"
numeric_integer_sum <- c(
is.numeric(foo) + is.integer(foo),
is.numeric(bar) + is.integer(bar),
is.numeric(baz) + is.integer(baz),
is.numeric(qux) + is.integer(qux),
is.numeric(quux) + is.integer(quux)
)
factor_result <- factor(numeric_integer_sum, levels = c(0, 1, 2))
factor_result
## [1] 2 2 0 0 1
## Levels: 0 1 2
as.numeric(factor_result)
## [1] 3 3 1 1 2
matrix_data <- matrix(c(2,3,4, 5,6,7, 8,9,10, 11,12,13), nrow = 3, byrow = TRUE)
as.vector(t(matrix_data))
## [1] 2 3 4 5 6 7 8 9 10 11 12 13
matrix_values <- matrix(c(34, 0, 1,
23, 1, 2,
33, 1, 1,
42, 0, 1,
41, 0, 2), nrow = 5, byrow = TRUE)
df_matrix <- as.data.frame(matrix_values)
df_matrix[[2]] <- as.logical(df_matrix[[2]])
df_matrix[[3]] <- as.factor(df_matrix[[3]])
df_matrix
## V1 V2 V3
## 1 34 FALSE 1
## 2 23 TRUE 2
## 3 33 TRUE 1
## 4 42 FALSE 1
## 5 41 FALSE 2
plot(-5:5, -5:5, type = "n", xlab = "", ylab = "", main = "")
arrows(0, 5, 0, -5, col = "black", lty = 2)
arrows(-5, 0, 5, 0, col = "black", lty = 2)
text(0, 0, "SOMETHING PROFOUND", cex = 1.5)

weight <- c(55, 85, 75, 42, 93, 63, 58, 75, 89, 67)
height <- c(161, 185, 174, 154, 188, 178, 170, 167, 181, 178)
sex <- c("female", "male", "male", "female", "male", "male", "female", "male", "male", "female")
sex_colors <- ifelse(sex == "male", "blue", "red")
plot(weight, height, col = sex_colors, pch = 19, xlab = "Weight (kg)", ylab = "Height (cm)", main = "Weight vs Height Scatter Plot")
legend("topright", legend = c("Male", "Female"), col = c("blue", "red"), pch = 19)

library(ggplot2)
weight <- c(55, 85, 75, 42, 93, 63, 58, 75, 89, 67)
height <- c(161, 185, 174, 154, 188, 178, 170, 167, 181, 178)
sex <- c("female", "male", "male", "female", "male", "male", "female", "male", "male", "female")
data <- data.frame(Weight = weight, Height = height, Sex = sex)
ggplot(data, aes(x = Weight, y = Height, color = Sex)) +
geom_point(size = 3) +
labs(title = "Weight vs Height Scatter Plot", x = "Weight (kg)", y = "Height (cm)") +
scale_color_manual(values = c("male" = "blue", "female" = "red")) +
theme_minimal()

data(quakes)
q5_df <- subset(quakes, mag >= 5)
write.table(q5_df, file = "q5.txt", sep = "!", row.names = FALSE, col.names = TRUE)
q5_dframe <- read.table("q5.txt", sep = "!", header = TRUE)
library(car)
## Loading required package: carData
data(Duncan)
str(Duncan)
## 'data.frame': 45 obs. of 4 variables:
## $ type : Factor w/ 3 levels "bc","prof","wc": 2 2 2 2 2 2 2 2 3 2 ...
## $ income : int 62 72 75 55 64 21 64 80 67 72 ...
## $ education: int 86 76 92 90 86 84 93 100 87 86 ...
## $ prestige : int 82 83 90 76 90 87 93 90 52 88 ...
plot(Duncan$education, Duncan$income,
xlab = "Education", ylab = "Income",
xlim = c(0, 100), ylim = c(0, 100),
pch = ifelse(Duncan$prestige <= 80, 16, 17),
col = ifelse(Duncan$prestige <= 80, "black", "blue"))
legend("topleft", legend = c("Prestige <= 80", "Prestige > 80"),
col = c("black", "blue"), pch = c(16, 17))

jpeg("education_vs_income_plot.jpg", width = 500, height = 500)
plot(Duncan$education, Duncan$income,
xlab = "Education", ylab = "Income",
xlim = c(0, 100), ylim = c(0, 100),
pch = ifelse(Duncan$prestige <= 80, 16, 17),
col = ifelse(Duncan$prestige <= 80, "black", "blue"))
legend("topleft", legend = c("Prestige <= 80", "Prestige > 80"),
col = c("black", "blue"), pch = c(16, 17))
dev.off()
## png
## 2
dataframes_list <- list(quakes = quakes, q5_dframe = q5_dframe, Duncan = Duncan)
capture.output(dataframes_list, file = "Exercise8.1.txt")
dput(dataframes_list, file = "Exercise8.1.txt")
dataframes_list <- dget("Exercise8.1.txt")
sapply(dataframes_list, is.data.frame)
## quakes q5_dframe Duncan
## TRUE TRUE TRUE
tiff("output_plot.tiff", width = 500, height = 500)
plot(Duncan$education, Duncan$income, xlab = "Education", ylab = "Income")
dev.off()
## png
## 2
methods_items <- ls("package:methods")
first_20_items <- head(methods_items, 20)
first_20_items
## [1] "addNextMethod" "allNames" "Arith"
## [4] "as" "as<-" "asMethodDefinition"
## [7] "assignClassDef" "assignMethodsMetaData" "balanceMethodsList"
## [10] "body<-" "cacheGenericsMetaData" "cacheMetaData"
## [13] "cacheMethod" "callGeneric" "callNextMethod"
## [16] "canCoerce" "cbind2" "checkAtAssignment"
## [19] "checkSlotAssignment" "classesToAM"
total_items <- length(methods_items)
total_items
## [1] 201
environment_read_table <- environmentName(environment(read.table))
environment_data <- environmentName(environment(data))
environment_matrix <- environmentName(environment(matrix))
environment_jpeg <- environmentName(environment(jpeg))
environment_read_table
## [1] "utils"
environment_data
## [1] ""
environment_matrix
## [1] "base"
environment_jpeg
## [1] "grDevices"
is_graphics <- environmentName(environment(smoothScatter)) == "graphics"
is_graphics
## [1] TRUE
sequence <- seq(-4, 4, 0.2)
sequence
## [1] -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2
## [16] -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8
## [31] 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0
array(8:1, dim=c(2,2,2))
## , , 1
##
## [,1] [,2]
## [1,] 8 6
## [2,] 7 5
##
## , , 2
##
## [,1] [,2]
## [1,] 4 2
## [2,] 3 1
dim=c(2,2,2)
rep(1:2, 3)
## [1] 1 2 1 2 1 2
seq(from=10, to=8, length=5)
## [1] 10.0 9.5 9.0 8.5 8.0
sort(decreasing=T, x=c(2,1,1,2,0.3,3,1.3))
## [1] 3.0 2.0 2.0 1.3 1.0 1.0 0.3
which(matrix(c(T, F, T, F), 2, 2))
## [1] 1 3
which(matrix(c(T, F, T, F), 2, 2))
## [1] 1 3
?plot.default
## starting httpd help server ...
## done
vec1 <- c(2, 1, 1, 3, 2, 1, 0)
vec2 <- c(3, 8, 2, 2, 0, 0, 0)
result <- ifelse(vec1 + vec2 > 3, vec1 * vec2, vec1 + vec2)
print(result)
## [1] 6 8 3 6 2 1 0
replace_diagonal <- function(mymat) {
diag_indices <- seq(1, nrow(mymat) * ncol(mymat), by = nrow(mymat) + 1)
diagonal_values <- mymat[diag_indices]
if (any(tolower(substring(diagonal_values, 1, 1)) == "g")) {
mymat[diag_indices] <- "HERE"
} else {
mymat <- diag(nrow(mymat))
}
return(mymat)
}
mymat1 <- matrix(as.character(1:16), 4, 4)
mymat2 <- matrix(c("DANDELION", "Hyacinthus", "Gerbera",
"MARIGOLD", "geranium", "Ligularia",
"Pachysandra", "SNAPDRAGON", "GLADIOLUS"), 3, 3, byrow = TRUE)
mymat3 <- matrix(c("GREAT", "exercises", "right", "here"), 2, 2, byrow = TRUE)
print(replace_diagonal(mymat1))
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] 0 1 0 0
## [3,] 0 0 1 0
## [4,] 0 0 0 1
print(replace_diagonal(mymat2))
## [,1] [,2] [,3]
## [1,] "HERE" "Hyacinthus" "Gerbera"
## [2,] "MARIGOLD" "HERE" "Ligularia"
## [3,] "Pachysandra" "SNAPDRAGON" "HERE"
print(replace_diagonal(mymat3))
## [,1] [,2]
## [1,] "HERE" "exercises"
## [2,] "right" "HERE"
vec1 <- c(2, 1, 1, 3, 2, 1, 0)
vec2 <- c(3, 8, 2, 2, 0, 0, 0)
if((vec1[1] + vec2[2]) == 10) { cat("Print me!") }
## Print me!
if(vec1[1] > 2 && vec2[1] >= 2) { cat("Print me!") }
if(all((vec2 - vec1)[c(2, 6)] < 7)) { cat("Print me!") }
if(!is.na(vec2[3])) { cat("Print me!") }
## Print me!
vec1 <- c(2, 1, 1, 3, 2, 1, 0)
vec2 <- c(3, 8, 2, 2, 0, 0, 0)
result <- ifelse((vec1 + vec2) > 3, vec1 * vec2, vec1 + vec2)
print(result)
## [1] 6 8 3 6 2 1 0
replace_diagonal <- function(mymat) {
diag_elems <- diag(mymat)
idx_g <- grepl("^g", diag_elems, ignore.case = TRUE)
if (any(idx_g)) {
diag(mymat)[idx_g] <- "HERE"
} else {
mymat <- diag(nrow(mymat))
}
return(mymat)
}
mymat1 <- matrix(as.character(1:16), 4, 4)
mymat2 <- matrix(c("DANDELION", "Hyacinthus", "Gerbera",
"MARIGOLD", "geranium", "Ligularia",
"Pachysandra", "SNAPDRAGON", "GLADIOLUS"), 3, 3)
mymat3 <- matrix(c("GREAT", "exercises", "right", "here"), 2, 2, byrow = TRUE)
replace_diagonal(mymat1)
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] 0 1 0 0
## [3,] 0 0 1 0
## [4,] 0 0 0 1
replace_diagonal(mymat2)
## [,1] [,2] [,3]
## [1,] "DANDELION" "MARIGOLD" "Pachysandra"
## [2,] "Hyacinthus" "HERE" "SNAPDRAGON"
## [3,] "Gerbera" "Ligularia" "HERE"
replace_diagonal(mymat3)
## [,1] [,2]
## [1,] "HERE" "exercises"
## [2,] "right" "here"
x <- 5
if (x == 3) {
cat("x is 3")
} else if (x == 5) {
cat("x is 5")
} else {
cat("x is not 3 or 5")
}
## x is 5
load <- c(2.5, 3.5, 10)
modem <- c(9, 25, 35)
highdose <- c(5, 15, 35)
doslevel <- factor(c("low", "high", "high", "high", "med", "med", "med", "low", "med", "high"))
z <- ifelse(load >= 10, modem / 2, load + highdose)
print(z)
## [1] 7.5 18.5 17.5
dosage <- ifelse(modem < 15 & load < 15, z + load, z * highdose)
print(dosage)
## [1] 10.0 277.5 612.5
loopvec1 <- c(2, 4, 6)
loopvec2 <- c(3, 5, 7)
mat <- matrix(0, nrow = length(loopvec1), ncol = length(loopvec2))
for (i in 1:length(loopvec1)) {
for (j in 1:length(loopvec2)) {
mat[i, j] <- loopvec1[i] * loopvec2[j]
}
}
loopvec1 <- c(2, 4, 6)
loopvec2 <- c(3, 5, 7)
mat <- matrix(0, nrow = length(loopvec1), ncol = length(loopvec2))
for (k in 1:(length(loopvec1) * length(loopvec2))) {
i <- ceiling(k / length(loopvec2))
j <- k %% length(loopvec2)
if (j == 0) j <- length(loopvec2)
mat[i, j] <- loopvec1[i] * loopvec2[j]
}
print(mat)
## [,1] [,2] [,3]
## [1,] 6 10 14
## [2,] 12 20 28
## [3,] 18 30 42
character_to_numeric <- function(vec) {
sapply(vec, function(x) {
switch(x,
"Homer" = 12,
"Marge" = 34,
"Bart" = 56,
"Lisa" = 78,
"Maggie" = 90,
NA)
})
}
test_vec <- c("Peter", "Homer", "Lois", "Stewie", "Maggie", "Bart")
result <- character_to_numeric(test_vec)
print(result)
## Peter Homer Lois Stewie Maggie Bart
## NA 12 NA NA 90 56
count_matrices <- function(lst) {
count <- 0
for (item in lst) {
if (is.matrix(item)) {
count <- count + 1
} else if (is.list(item)) {
count <- count + count_matrices(item)
}
}
return(count)
}
mylist1 <- list(aac = matrix(3:4, 1, 2), bb = matrix(1:4, 2, 2),
cc = matrix(c(T, T, F, T, F, F), 3, 2), dd = "string here",
ee = list(matrix(c("hello", "you"), 1, 2)),
ff = matrix(c("red", "green", "blue", "yellow"), 2, 2))
mylist2 <- list("tricked you", as.vector(matrix(1:6, 3, 2)))
mylist3 <- list(list(matrix(1:2, 1, 2), matrix(c(3, 2), 2, 2)),
list(c(1, 2), matrix(c(1, 2), 1, 2)), rbind(1:10, 10:91))
## Warning in rbind(1:10, 10:91): number of columns of result is not a multiple of
## vector length (arg 1)
count1 <- count_matrices(mylist1)
count2 <- count_matrices(mylist2)
count3 <- count_matrices(mylist3)
cat("Count of matrices in mylist1:", count1, "\n")
## Count of matrices in mylist1: 5
cat("Count of matrices in mylist2:", count2, "\n")
## Count of matrices in mylist2: 0
cat("Count of matrices in mylist3:", count3, "\n")
## Count of matrices in mylist3: 4
xnames <- c(2, 2.5)
ynames <- c(0.5, 2.5, 5)
znames <- c(6, 7, 8, 9)
xnames <- c(2, 2.5)
ynames <- c(0.5, 2.5, 5)
znames <- c(6, 7, 8, 9)
mymatlist <- list(
xmat = outer(xnames, xnames, FUN = "+"),
ymat = outer(ynames, ynames, FUN = "+"),
zmat = outer(znames, znames, FUN = "+")
)
for (i in 1:length(mymatlist)) {
mymatlist[[i]] <- mymatlist[[i]] + 1
}
print(mymatlist)
## $xmat
## [,1] [,2]
## [1,] 5.0 5.5
## [2,] 5.5 6.0
##
## $ymat
## [,1] [,2] [,3]
## [1,] 2.0 4.0 6.5
## [2,] 4.0 6.0 8.5
## [3,] 6.5 8.5 11.0
##
## $zmat
## [,1] [,2] [,3] [,4]
## [1,] 13 14 15 16
## [2,] 14 15 16 17
## [3,] 15 16 17 18
## [4,] 16 17 18 19
factorial_loop <- function(n) {
result <- 1
while (n > 0) {
result <- result * n
n <- n - 1
}
return(result)
}
cat("Factorial of 3 is:", factorial_loop(3), "\n")
## Factorial of 3 is: 6
cat("Factorial of 7 is:", factorial_loop(7), "\n")
## Factorial of 7 is: 5040
mystring <- "C is fun!"
count1 <- 0
count2 <- 0
for (i in 1:nchar(mystring)) {
current_char <- substr(mystring, i, i)
if (i == 2 || i == (nchar(mystring) - 1)) {
count1 <- count1 + 1
}
if (current_char %in% c(" ", "!")) {
count2 <- count2 + 1
}
}
cat("Count1 (second and second-to-last characters):", count1, "\n")
## Count1 (second and second-to-last characters): 2
cat("Count2 (spaces or special characters):", count2, "\n")
## Count2 (spaces or special characters): 3
matlist <- list(matrix(c(T, F, T, T), 2, 2),
matrix(c("a", "r", "c", "b", "z", "p", "q"), 3, 2),
matrix(1:8, 2, 4))
## Warning in matrix(c("a", "r", "c", "b", "z", "p", "q"), 3, 2): data length [7]
## is not a sub-multiple or multiple of the number of rows [3]
matlist <- lapply(matlist, t)
matlist
## [[1]]
## [,1] [,2]
## [1,] TRUE FALSE
## [2,] TRUE TRUE
##
## [[2]]
## [,1] [,2] [,3]
## [1,] "a" "r" "c"
## [2,] "b" "z" "p"
##
## [[3]]
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
## [4,] 7 8
matlist <- list(matrix(c(T, F, T, T), 2, 2),
matrix(c("a", "r", "c", "b", "z", "p", "q"), 3, 2),
matrix(1:8, 2, 4))
## Warning in matrix(c("a", "r", "c", "b", "z", "p", "q"), 3, 2): data length [7]
## is not a sub-multiple or multiple of the number of rows [3]
matlist <- lapply(matlist, t)
matlist
## [[1]]
## [,1] [,2]
## [1,] TRUE FALSE
## [2,] TRUE TRUE
##
## [[2]]
## [,1] [,2] [,3]
## [1,] "a" "r" "c"
## [2,] "b" "z" "p"
##
## [[3]]
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
## [3,] 5 6
## [4,] 7 8
qux <- array(96:1, dim = c(4, 4, 2, 3))
diags <- apply(qux[, , 2, ], 3, diag)
diags
## [,1] [,2] [,3]
## [1,] 80 48 16
## [2,] 75 43 11
## [3,] 70 38 6
## [4,] 65 33 1
dimsums <- apply(qux[, 4, , ], 3, rowSums)
dimsums
## [,1] [,2] [,3]
## [1,] 152 88 24
## [2,] 150 86 22
## [3,] 148 84 20
## [4,] 146 82 18
foo <- 5
bar <- c(2, 3, 1, 1, 4, 0, 4, 1, 3)
i <- 1
while (i <= length(bar) && bar[i] < foo) {
print(bar[i])
i <- i + 1
}
## [1] 2
## [1] 3
## [1] 1
## [1] 1
## [1] 4
## [1] 0
## [1] 4
## [1] 1
## [1] 3
foo <- 5
bar <- c(2, 3, 1, 1, 4, 0, 4, 1, 3)
result <- ifelse(bar < foo, bar, NA)
result[!is.na(result)]
## [1] 2 3 1 1 4 0 4 1 3
mynumbers <- c(1, 5, 1, 2, 6, 4, 6, 6, 2)
resultlist <- list()
for (i in seq_along(mynumbers)) {
if (mynumbers[i] > 5) {
break
}
resultlist[[i]] <- matrix(1:mynumbers[i], nrow=mynumbers[i], ncol=mynumbers[i])
}
resultlist
## [[1]]
## [,1]
## [1,] 1
##
## [[2]]
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 1 1 1 1
## [2,] 2 2 2 2 2
## [3,] 3 3 3 3 3
## [4,] 4 4 4 4 4
## [5,] 5 5 5 5 5
##
## [[3]]
## [,1]
## [1,] 1
##
## [[4]]
## [,1] [,2]
## [1,] 1 1
## [2,] 2 2
i <- 1
resultlist <- list()
repeat {
if (mynumbers[i] > 5) break
resultlist[[i]] <- matrix(1:mynumbers[i], nrow=mynumbers[i], ncol=mynumbers[i])
i <- i + 1
}
resultlist
## [[1]]
## [,1]
## [1,] 1
##
## [[2]]
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 1 1 1 1
## [2,] 2 2 2 2 2
## [3,] 3 3 3 3 3
## [4,] 4 4 4 4 4
## [5,] 5 5 5 5 5
##
## [[3]]
## [,1]
## [1,] 1
##
## [[4]]
## [,1] [,2]
## [1,] 1 1
## [2,] 2 2
matlist1 <- list(
matrix(1:4, 2, 2),
matrix(5:2, 2, 2),
matrix(8:5, 2, 2)
)
matlist2 <- list(matrix(1:4, 2, 2), matrix(2:5, 2, 2), matrix(8:3, 2, 2))
## Warning in matrix(8:3, 2, 2): data length differs from size of matrix: [6 != 2
## x 2]
reslist <- vector("list", length(matlist1) * length(matlist2))
counter <- 1
for (i in seq_along(matlist1)) {
for (j in seq_along(matlist2)) {
if (all(dim(matlist1[[i]]) == dim(matlist2[[j]]))) {
reslist[[counter]] <- matlist1[[i]] %*% matlist2[[j]]
} else {
reslist[[counter]] <- "not possible"
}
counter <- counter + 1
}
}
reslist
## [[1]]
## [,1] [,2]
## [1,] 7 15
## [2,] 10 22
##
## [[2]]
## [,1] [,2]
## [1,] 11 19
## [2,] 16 28
##
## [[3]]
## [,1] [,2]
## [1,] 29 21
## [2,] 44 32
##
## [[4]]
## [,1] [,2]
## [1,] 11 27
## [2,] 8 20
##
## [[5]]
## [,1] [,2]
## [1,] 19 35
## [2,] 14 26
##
## [[6]]
## [,1] [,2]
## [1,] 61 45
## [2,] 46 34
##
## [[7]]
## [,1] [,2]
## [1,] 20 48
## [2,] 17 41
##
## [[8]]
## [,1] [,2]
## [1,] 34 62
## [2,] 29 53
##
## [[9]]
## [,1] [,2]
## [1,] 106 78
## [2,] 91 67