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:

  1. sample_vector <- c(1, 2, NA, 4, NA, 6): A sample vector is created containing numeric values along with NA (representing null values).

  2. is.na(sample_vector): The is.na() function checks each element in the sample_vector to determine if it's a null value (NA). It returns a logical vector where TRUE represents a null value and FALSE represents a non-null value.

  3. sum(is.na(sample_vector)): The sum() function adds up all the TRUE values in the logical vector obtained from is.na(), effectively counting the number of null values present in the sample_vector.

  4. print(null_count): Finally, the count of null values (null_count) is displayed as output.