A numeric type in R represents numbers that can have decimal points. It is used to store real numbers like 3.14 or 2.0.
Click to reveal answer
beginner
How do you create a numeric variable in R?
You can create a numeric variable by assigning a number with or without decimals, for example: x <- 5 or y <- 3.14.
Click to reveal answer
beginner
What function checks if a value is numeric in R?
The function is.numeric() returns TRUE if the value is numeric, otherwise FALSE.
Click to reveal answer
intermediate
What happens if you add a numeric and an integer in R?
R automatically converts the integer to numeric and performs the addition, returning a numeric result.
Click to reveal answer
intermediate
How can you convert a character string to numeric in R?
Use the as.numeric() function to convert a character string that represents a number into a numeric type.
Click to reveal answer
Which of these is a numeric value in R?
Afactor(3)
B3.14
CTRUE
D"3.14"
✗ Incorrect
3.14 is a numeric value. "3.14" is a character string, TRUE is logical, and factor(3) is a factor.
What does is.numeric(5L) return in R?
ATRUE
BFALSE
CError
DNA
✗ Incorrect
5L is an integer, but integers are considered numeric in R, so is.numeric returns TRUE.
How do you convert a character "42" to numeric in R?
Anumeric("42")
Bas.character(42)
Cto.numeric("42")
Das.numeric("42")
✗ Incorrect
The correct function to convert character to numeric is as.numeric().
What is the result of 2 + 3.5 in R?
A5.5
B5
C"5.5"
DError
✗ Incorrect
R converts 2 (integer) to numeric and adds 3.5, resulting in 5.5.
Which function checks if a value is numeric?
Ais.integer()
Bis.character()
Cis.numeric()
Dis.logical()
✗ Incorrect
is.numeric() checks if a value is numeric.
Explain what a numeric type is in R and how it differs from integer.
Think about numbers with and without decimal points.
You got /4 concepts.
Describe how to convert a character string to a numeric value in R and why this might be useful.
Conversion helps when data is read as text but you need numbers.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is TRUE about numeric types in R?
easy
A. Numeric and integer types are the same in R.
B. Integer type stores decimal numbers by default.
C. Numeric type stores decimal numbers by default.
D. You must always use quotes to create numeric values.
Solution
Step 1: Understand numeric and integer types in R
In R, numeric type stores decimal numbers by default, while integer type stores whole numbers with an L suffix.
Step 2: Evaluate each option
Numeric type stores decimal numbers by default correctly states that numeric stores decimals by default. Options A, B, and C are incorrect based on R's type system.
Final Answer:
Numeric type stores decimal numbers by default. -> Option C
2. Which of the following is the correct way to create an integer value 5 in R?
easy
A. x <- 5L
B. x <- "5"
C. x <- 5
D. x <- as.integer("5L")
Solution
Step 1: Recall integer creation syntax in R
In R, to create an integer literal, you add L after the number, e.g., 5L.
Step 2: Check each option
x <- 5L uses 5L which is correct. x <- 5 creates a numeric (decimal) 5, not integer. x <- "5" creates a string. x <- as.integer("5L") fails to convert the string "5L" to integer due to the L suffix, resulting in NA.
Final Answer:
x <- 5L -> Option A
Quick Check:
Integer literal = number + L [OK]
Hint: Use L suffix to make integers, else numeric by default [OK]
Common Mistakes:
Using plain numbers for integers
Confusing strings with numbers
Using as.integer on decimals expecting exact integer
3. What is the output of the following R code?
x <- as.numeric("3.14") typeof(x)
medium
A. "double"
B. "numeric"
C. "integer"
D. "character"
Solution
Step 1: Understand as.numeric() conversion
as.numeric() converts a string to a numeric type, which in R is stored as double precision floating point.
Step 2: Check typeof() result for numeric
typeof() returns "double" for numeric values with decimals in R.
Final Answer:
"double" -> Option A
Quick Check:
as.numeric() returns double type [OK]
Hint: as.numeric() returns double type, not integer [OK]
Common Mistakes:
Expecting integer type from as.numeric()
Confusing character and numeric types
Thinking numeric means integer
4. Identify the error in this R code:
y <- 10L z <- "abc" sum <- y + z
medium
A. Error because sum is a reserved word
B. No error, sum is 10
C. Error because 10L is not numeric
D. Error because "abc" cannot convert to numeric
Solution
Step 1: Check addition of integer and character
z <- "abc" creates a character value. Adding integer y (10L) and character z causes an error because character cannot be converted to numeric for arithmetic operations.
Step 2: Confirm the error
The error is 'non-numeric argument to binary operator' since "abc" cannot convert to numeric.
Final Answer:
Error because "abc" cannot convert to numeric -> Option D
Quick Check:
String in arithmetic causes error [OK]
Hint: Non-numeric strings to as.numeric() produce NA [OK]
Common Mistakes:
Trying to perform arithmetic on strings
Thinking 10L is not numeric
Assuming sum is reserved and causes error
5. You have a character vector c("1", "2.5", "3L", "four"). Which R code correctly converts only the valid numeric values to numeric type, ignoring invalid ones?
hard
A. as.numeric(c("1", "2.5", "3L", "four"))
B. as.numeric(gsub("L", "", c("1", "2.5", "3L", "four")))
C. suppressWarnings(as.numeric(c("1", "2.5", "3L", "four")))
D. as.integer(c("1", "2.5", "3L", "four"))
Solution
Step 1: Understand the problem with "3L" and "four"
"3L" is not directly numeric because of the L suffix as a string; "four" is invalid.
Step 2: Use gsub to remove "L" and convert
Removing "L" from strings allows as.numeric() to convert "3" correctly; "four" becomes NA.
Step 3: Check options
as.numeric(gsub("L", "", c("1", "2.5", "3L", "four"))) removes "L" before conversion, correctly converting valid numbers and producing NA for "four". Others either do not handle "3L" or convert incorrectly.
Final Answer:
as.numeric(gsub("L", "", c("1", "2.5", "3L", "four"))) -> Option B
Quick Check:
Remove non-numeric chars before conversion [OK]
Hint: Remove non-numeric chars before as.numeric() [OK]
Common Mistakes:
Trying to convert strings with letters directly
Using as.integer on decimals
Ignoring warnings from invalid conversions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.