R Programming Fundamentals, SQL, and Advanced Clustering Methods
Posted by Anonymous and classified in Mathematics
Written on in
with a size of 238.95 KB
Section A: R Basics and Data Types (Weeks 1-4)
Model Questions and Answers
Q: Create a vector v (3, NA, Inf, -Inf). Explain adding 5 to v.
A: The operation is element-wise. Missing values (NA) propagate, resulting in NA. Infinite values (Inf, -Inf) remain infinite.
v <- c(3, NA, Inf, -Inf)
print(v + 5)
# Output: [1] 8 NA Inf -InfQ: Given vector a, write code to count and replace NAs.
A: Assuming a <- c(10, 15, NA, 20).
- Count NAs:
sum(is.na(a))→ 1. - Replace NAs (e.g., with 0):
a[is.na(a)] <- 0.
Q: Explain the difference between a[a > 12] and a[which(a > 12)].
A: Both select elements greater than 12 (15, 20). However:
a[a > 12]→[1] 15 NA(Uses logical indexing; preserves the position of NA in the original vector as NA).a[which(