How can I know how many values are NA in a dataset? OR if there are any NAs and NaNs in dataset?
Asked
Active
Viewed 3.2k times
7
-
1`anyNA`, `complete.cases`, and `na.omit` are all useful functions. Your question is not very specific and clearly off-topic. – Roland Mar 16 '15 at 10:59
-
There is not function named anyNA in R. Can you tell me how you used it? – Mar 16 '15 at 11:06
-
1If there is no `anyNA` in your R version, you should update R. – Roland Mar 16 '15 at 11:48
-
I'm voting to close this question as off-topic because it is a basic question about using R. See advice on software-related questions in the Help Center. – Nick Cox Mar 16 '15 at 12:11
-
I'm voting to close this question as off-topic because it is about how to use r without a reproducible example. – gung - Reinstate Monica Mar 16 '15 at 12:11
-
Does this answer your question? [Determine the number of NA values in a column](https://stackoverflow.com/questions/24027605/determine-the-number-of-na-values-in-a-column) – UseR10085 May 23 '20 at 10:03
4 Answers
2
As @Roland noticed there are multiple functions for finding and dealing with missing values in R (see help("NA")
and here).
Example:
Create a fake dataset with some NA
's:
data <- matrix(1:300,,3)
data[sample(300, 40)] <- NA
Check if there are any missing values:
anyNA(data)
Columnwise check if there are any missing values:
apply(data, 2, anyNA)
Check percentages and counts of missing values in columns:
colMeans(is.na(data))*100
colSums(is.na(data))

Tim
- 7,075
- 6
- 29
- 58
-
2Don't use `apply` when it's not needed. Just `colSums(is.na(data))` works. – nicola Mar 16 '15 at 12:48
0
For a dataframe it is:
sum(is.na(df)
here df is the dataframe
where as for a particular column in the dataframe you can use:
sum(is.na(df$col)
or
cnt=0
for(i in df$col){
if(is.na(i)){
cnt=cnt+1
}
}
cnt
here cnt gives the no. of NA in the column

Jayanth
- 27
- 1
- 5
0
You can simply get the number of "NA" included in the each column of dataset by using R.
For a vector x
summary(x)
For a data frame df
summary(df)

7-x
- 3
- 2