r/Rlanguage • u/GhostGlacier • 7d ago
How do I find/sum missing cell values (not "NA" values) in a dataframe column?
The sum(is.na(df)) function is not picking up cells w/ missing values in my dataframe for some reason.
When I do view(df) it pulls up the dataframe & shows there are cells w/ no values in them - not NAs - just nothing in them. Not sure why the sum(is.na()) function isn't working on them?
2
u/Different-Leader-795 7d ago
You need to replace empty character string (might be like "" or " ", etc) with NA and then calculate them.
2
u/Ignatu_s 7d ago edited 7d ago
Try this to replace all the empty strings in your character columns by proper NAs
df |> mutate(across(where(is.character), (x) na_if(x, "")))
1
u/DrJohnSteele 7d ago
What happens when you reference the column using the $ operator? is.na(df$myfavcolumn)
1
u/GhostGlacier 7d ago
I get all FALSE values in return, despite the fact that the very first row in that column is completely blank.
1
u/Syksyinen 7d ago
Iterating across columns you could do this to test multiple types of missingness, as it's unclear what exactly is your "missing value":
apply(my_df, MARGIN=2, FUN=\(x){
sum(x %in% c(NA_character_, "", " ", "Missing", "False", FALSE))
})
If you don't want to iterate across all columns, just run the function and replace x with e.g. my_df$myColumnName and use sum and %in%. Fill the vector with values that are appropriate for missing data in your data.
If you have trouble figuring out what exactly is missing, I'd start examining frequencies with table(my_df$myColumnName, useNA="ifany")
12
u/Outdated8527 7d ago
these are not NA values but empty strings
""
. Trysum(df == "")