0
0
R Programmingprogramming~20 mins

NULL and NA values in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NULL and NA Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of NA and NULL in vector length
What is the output of the following R code?
R Programming
x <- c(1, 2, NA, NULL, 5)
length(x)
A4
B5
C3
DError
Attempts:
2 left
💡 Hint
Remember that NULL values are removed when combined in vectors.
Predict Output
intermediate
2:00remaining
Comparing NA and NULL with is.na() and is.null()
What is the output of this R code?
R Programming
a <- NA
b <- NULL
is.na(a)
is.na(b)
is.null(a)
is.null(b)
A
[1] TRUE
logical(0)
[1] FALSE
[1] TRUE
B
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
C
[1] TRUE
[1] FALSE
[1] TRUE
[1] FALSE
D
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE
Attempts:
2 left
💡 Hint
Check how is.na() and is.null() behave differently on NA and NULL.
🔧 Debug
advanced
2:00remaining
Why does this NA check fail?
Consider this R code snippet: x <- c(1, 2, NA, 4) if (x == NA) { print("Has NA") } else { print("No NA") } Why does this code not print "Has NA" even though x contains NA?
R Programming
x <- c(1, 2, NA, 4)
if (x == NA) {
  print("Has NA")
} else {
  print("No NA")
}
ABecause the vector x does not contain NA values.
BBecause NA is treated as zero in comparisons.
CBecause comparing with NA using == always returns NA, not TRUE or FALSE.
DBecause the if statement only checks the first element of x.
Attempts:
2 left
💡 Hint
Think about how NA behaves in logical comparisons.
🧠 Conceptual
advanced
2:00remaining
Difference between NULL and NA in R
Which statement correctly describes the difference between NULL and NA in R?
ABoth NULL and NA are identical and can be used interchangeably.
BNA is used to remove elements from a vector, NULL is a missing value marker.
CNULL is a numeric missing value, NA is a character missing value.
DNULL represents absence of a value or object, while NA represents a missing value within an object.
Attempts:
2 left
💡 Hint
Think about how NULL and NA behave in vectors and lists.
Predict Output
expert
2:00remaining
Count of NA and NULL in a list
What is the output of this R code?
R Programming
lst <- list(a = 1, b = NA, c = NULL, d = 4)
sum(unlist(sapply(lst, is.na)))
length(lst)
A
1
3
B
1
4
C
0
3
D
0
4
Attempts:
2 left
💡 Hint
Remember that NULL elements are kept in lists (length 4). is.na(NULL) returns logical(0), which unlist() drops.