null count in r
# Create a sample vector with null values
sample_vector <- c(1, 2, NA, 4, NA, 6)
# Use is.na() function to identify null values in the vector
null_count <- sum(is.na(sample_vector))
# Display the count of null values
print(null_count)
Explanation of Steps:
sample_vector <- c(1, 2, NA, 4, NA, 6)
: A sample vector is created containing numeric values along with NA (representing null values).is.na(sample_vector)
: Theis.na()
function checks each element in thesample_vector
to determine if it's a null value (NA). It returns a logical vector whereTRUE
represents a null value andFALSE
represents a non-null value.sum(is.na(sample_vector))
: Thesum()
function adds up all theTRUE
values in the logical vector obtained fromis.na()
, effectively counting the number of null values present in thesample_vector
.print(null_count)
: Finally, the count of null values (null_count
) is displayed as output.