Note: We can also use
which()
function to extract exact indexes ofNA
values.
is.nan(x)
# is.na in r examplex = c(1, 2, NA, 4, NA, 6, 7)# invoking is.na() to get NA's indexesprint(is.na(x))# show indexes of NA'sprint(which(is.na(x)))
We can also use is.na
()
to find the missing values in a dataframe and then replace them with new value.
Find missing values.
# Create a vector with missing valuesdf <- c(1, 2, NA, 4, NA, 6)# Filter out missing valuesnew_df <- df[!is.na(df)] # new data frame with no missing valuesprint(new_df)# Output: 1 2 4 6
Replace missing values with other values.
df <- c(1, 2, NA, 4, NA, 6)# Replace missing values with 0df_new<- ifelse(is.na(df), 0, df)print(df_new)# Output: 1 2 0 4 0 6
Count the number of missing values.
df <- c(1, 2, NA, 4, NA, 6)count <- sum(is.na(df))print(count)
Free Resources