Discussion Assignment Week 3
Esha Soni
01/22/25
Contents
Question 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Question 1
x <- 1:10
y <- 11:19
#1.a
x[3]+y[4]
## [1] 17
#1.b
sum(x[1:4])
## [1] 10
#1.c
(x[5]*x[10])/(y[8]-y[5])
## [1] 16.66667
#1.d
c(x[6],x[4],x[7],x[3])
## [1] 6 4 7 3
#1.e
c(y[5], rep(y[1],3), rep(y[4],2), y[7])
## [1] 15 11 11 11 14 14 17
1
#1.f
length(x)+length(y)
## [1] 19
##Question 2
x <- seq(from = -10, to = 20, by =2)
#2.a
log_vec <- x>5 & x<= -5
print(log_vec)
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE
#2.b
log_vec1 <- !(x %in% c(10,11,12))
print(log_vec1)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
## [13] TRUE TRUE TRUE TRUE
#2.c
non_neg <- sum(x>=0)
print(non_neg)
## [1] 11
#2.d
min_greater <- min(x[x>6])
print(min_greater)
## [1] 8
#2.e
abs_val <- abs(sum(x[x<0]))
print(abs_val)
## [1] 30
#2.f
is_na_y <- [Link](y)
print(is_na_y)
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##Problem 3
2
#test.a)
x <- c(2, 4, NA, NA, 9, NaN, 0)
cleaned_x <- (x[])
print(cleaned_x)
## [1] 2 4 9 0
#test.b)
x <- 5:9
cleaned_x <- (x[])
print(cleaned_x)
## [1] 5 6 7 8 9
#test.c)
x <- rep(NA, 10)
cleaned_x <- (x[])
print(cleaned_x)
## logical(0)
#test.d)
x <-character(0)
cleaned_x <- (x[])
print(cleaned_x)
## character(0)
##Problem 4
x <- 1:3
mean_x <- mean(x, [Link] = TRUE)
x[[Link](x) | [Link](x)] <- mean_x
print(x)
## [1] 1 2 3
x<- c(1, NA, 2, NaN, 3)
mean_x <- mean(x, [Link] = TRUE)
x[[Link](x) | [Link](x)] <- mean_x
print(x)
## [1] 1 2 2 2 3
x<- c(NA,NA,NA)
mean_x <- mean(x, [Link] = TRUE)
x[[Link](x) | [Link](x)] <- mean_x
print(x)
3
## [1] NA NA NA
##Problem 5
x <- -rev(seq_len(11))
#5.a)
idx_num <- seq_len(5)
x[(idx_num)]
## [1] -11 -10 -9 -8 -7
rem_removed <- x[-idx_num]
rem_last <- rem_removed[idx_num]
print(rem_last)
## [1] -6 -5 -4 -3 -2