0
0
R Programmingprogramming~20 mins

Type checking and conversion in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of type conversion with as.numeric()
What is the output of the following R code?
R Programming
x <- c("10", "20", "thirty", "40")
y <- as.numeric(x)
y
A[1] 10 20 NA 40
B[1] "10" "20" "thirty" "40"
C[1] 10 20 30 40
D[1] NA NA NA NA
Attempts:
2 left
💡 Hint
as.numeric() converts strings to numbers but returns NA for non-numeric strings.
Predict Output
intermediate
2:00remaining
Result of is.integer() on numeric vector
What does the following R code print?
R Programming
v <- c(1, 2, 3)
is.integer(v)
A[1] TRUE
B[1] NA
C[1] FALSE
DError: object 'v' not found
Attempts:
2 left
💡 Hint
In R, numeric vectors are by default of type double, not integer.
Predict Output
advanced
2:00remaining
Behavior of as.character() on factor
What is the output of this R code?
R Programming
f <- factor(c("apple", "banana", "apple"))
char_vec <- as.character(f)
char_vec
A[1] "apple" "banana" "apple"
B[1] 1 2 1
C[1] "1" "2" "1"
D[1] NA NA NA
Attempts:
2 left
💡 Hint
as.character() converts factor levels back to their string values.
Predict Output
advanced
2:00remaining
Output of typeof() on logical conversion
What is the output of this R code?
R Programming
x <- c(TRUE, FALSE, TRUE)
t <- as.integer(x)
t
typeof(t)
A
[1] 1 0 1
[1] "double"
B
[1] 1 0 1
[1] "integer"
C
[1] TRUE FALSE TRUE
[1] "logical"
D
[1] NA NA NA
[1] "integer"
Attempts:
2 left
💡 Hint
TRUE converts to 1 and FALSE to 0 when cast to integer.
Predict Output
expert
3:00remaining
Result of coercion in mixed-type vector
What is the output of this R code?
R Programming
v <- c(1L, 2.5, TRUE, "4")
v
typeof(v)
A
[1] 1 2.5 1 NA
[1] "double"
B
[1] 1 2 1 4
[1] "integer"
C
[1] 1 2.5 1 4
[1] "double"
D
[1] "1" "2.5" "TRUE" "4"
[1] "character"
Attempts:
2 left
💡 Hint
When combining different types, R coerces all elements to the most flexible type.