Reputation: 203
Suppose that I have a data.frame
as follows:
a b c
1 5 NA 6
2 NA NA 7
3 6 5 8
I would like to find the length of each column, excluding NA's. The answer should look like
a b c
2 1 3
So far, I've tried:
!is.na() # Gives TRUE/FALSE
length(!is.na()) # 9 -> Length of the whole matrix
dim(!is.na()) # 3 x 3 -> dimension of a matrix
na.omit() # removes rows with any NA in it.
Please tell me how can I get the required answer.
Upvotes: 9
Views: 30075
Reputation: 8523
Though the sum is probably a faster solution, I think that length(x[!is.na(x)])
is more readable, or even length(na.omit(x))
.
Upvotes: 7
Reputation: 89
If you wanna count non-NA values in the entire data frame, the following will help.
sum(!is.na(df))
[1] 3
then count non-NA values in each column as follows
colSums(!is.na(df))
a b c
2 1 3
Upvotes: 0
Reputation: 1
I tried NCOL instead of ncol and it worked.
> nrow(tsa$Region)
NULL
> NROW(tsa$Region)
[1] 27457
> ncol(tsa$Region)
NULL
> NCOL(tsa$Region)
[1] 1
Upvotes: -1