Ay_M
Ay_M

Reputation: 203

Length of columns excluding NA in r

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

Answers (5)

Dan Chaltiel
Dan Chaltiel

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

Fkiran
Fkiran

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

Paul Arada
Paul Arada

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

agstudy
agstudy

Reputation: 121626

Or faster :

colSums(!is.na(dat))
a b c 
2 1 3 

Upvotes: 12

user1609452
user1609452

Reputation: 4454

> apply(dat, 2, function(x){sum(!is.na(x))})
a b c 
2 1 3 

Upvotes: 3

Related Questions