What is the output of this R code?
x <- c(1, 2, 3) y <- c("a", "b", "c") z <- c(x, y) print(z)
Think about how R handles combining different data types in a vector.
When combining numeric and character vectors, R converts all elements to character type to keep the vector consistent.
What happens when you run this R code?
f <- factor(c(1, 2, 3)) result <- f + 1 print(result)
Consider what type a factor is and if arithmetic is allowed.
Factors are categorical and not numeric, so arithmetic operations on them cause an error.
Why does this R code produce an error?
x <- c(TRUE, FALSE, TRUE) y <- c(1, 0, 1) result <- x + y print(result)
Think about how R treats logical values in arithmetic.
Logical TRUE and FALSE are treated as 1 and 0 in arithmetic operations, so addition works element-wise.
What is the output of this R code?
lst <- list(a = 1:3, b = "hello") print(lst[1]) print(lst[[1]])
Recall the difference between single and double brackets for lists.
Single brackets return a sublist, double brackets extract the element itself.
Which of the following best explains why data types matter in R?
Think about how different data types affect calculations and memory.
Data types affect storage, operations, and behavior in R, so understanding them is key to writing correct and efficient code.