Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which function in R checks if a variable is of numeric type?
easy
A. as.character()
B. as.numeric()
C. is.character()
D. is.numeric()

Solution

  1. Step 1: Understand type checking functions

    Functions starting with is. check the type of a variable.
  2. Step 2: Identify the correct function for numeric check

    is.numeric() returns TRUE if the variable is numeric.
  3. Final Answer:

    is.numeric() -> Option D
  4. Quick Check:

    Type check numeric = is.numeric() [OK]
Hint: Use is.type() to check type, as.type() to convert [OK]
Common Mistakes:
  • Confusing is.numeric() with as.numeric()
  • Using as.character() to check type
  • Using is.character() for numeric check
2. Which of the following is the correct way to convert a character vector c("1", "2", "3") to numeric in R?
easy
A. is.numeric(c("1", "2", "3"))
B. as.numeric(c("1", "2", "3"))
C. as.character(c(1, 2, 3))
D. is.character(c(1, 2, 3))

Solution

  1. Step 1: Identify conversion functions

    as.numeric() converts data to numeric type.
  2. Step 2: Apply conversion to character vector

    Using as.numeric(c("1", "2", "3")) converts strings to numbers 1, 2, 3.
  3. Final Answer:

    as.numeric(c("1", "2", "3")) -> Option B
  4. Quick Check:

    Convert char to numeric = as.numeric() [OK]
Hint: Use as.numeric() to convert strings to numbers [OK]
Common Mistakes:
  • Using is.numeric() instead of as.numeric()
  • Trying to convert numeric to character for numeric conversion
  • Confusing is.character() with conversion functions
3. What will be the output of the following R code?
vec <- c("10", "20", "30")
num_vec <- as.numeric(vec)
is.numeric(num_vec)
medium
A. TRUE
B. FALSE
C. Error
D. NA

Solution

  1. Step 1: Convert character vector to numeric

    as.numeric(vec) changes strings "10", "20", "30" to numbers 10, 20, 30.
  2. Step 2: Check if converted vector is numeric

    is.numeric(num_vec) returns TRUE because num_vec is numeric.
  3. Final Answer:

    TRUE -> Option A
  4. Quick Check:

    as.numeric() converts, is.numeric() confirms [OK]
Hint: as.numeric() converts, is.numeric() checks type [OK]
Common Mistakes:
  • Expecting FALSE because original was character
  • Thinking as.numeric() returns character
  • Confusing is.numeric() with as.numeric()
4. Identify the error in the following R code snippet:
val <- "123abc"
num_val <- as.numeric(val)
print(num_val)
medium
A. No error, prints 123
B. Syntax error due to quotes
C. Prints NA with a warning
D. Prints 0 without warning

Solution

  1. Step 1: Attempt to convert mixed string to numeric

    as.numeric("123abc") cannot convert because of non-numeric characters.
  2. Step 2: Result of conversion

    It returns NA and shows a warning about NAs introduced by coercion.
  3. Final Answer:

    Prints NA with a warning -> Option C
  4. Quick Check:

    Invalid numeric conversion = NA + warning [OK]
Hint: Invalid numeric strings convert to NA with warning [OK]
Common Mistakes:
  • Expecting partial numeric conversion
  • Ignoring warning messages
  • Assuming syntax error due to quotes
5. You have a list lst <- list("5", 10, "abc", "15.5"). Which R code correctly converts all elements to numeric, replacing non-convertible values with 0?
hard
A. sapply(lst, function(x) if(is.na(as.numeric(x))) 0 else as.numeric(x))
B. sapply(lst, function(x) as.numeric(x))
C. sapply(lst, function(x) if(is.na(as.numeric(x))) as.numeric(x) else 0)
D. sapply(lst, function(x) if(is.numeric(x)) x else 0)

Solution

  1. Step 1: Convert each element to numeric and check for NA

    Use as.numeric(x) inside a function and check if result is NA.
  2. Step 2: Replace NA with 0, else keep numeric value

    If as.numeric(x) is NA, return 0; otherwise return the numeric value.
  3. Final Answer:

    sapply(lst, function(x) if(is.na(as.numeric(x))) 0 else as.numeric(x)) -> Option A
  4. Quick Check:

    Replace NA with 0 using conditional inside sapply [OK]
Hint: Use is.na(as.numeric(x)) to detect and replace [OK]
Common Mistakes:
  • Not checking for NA after conversion
  • Replacing numeric values incorrectly
  • Confusing condition logic in if-else
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard