Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple method of counting non-NAs in column of data String [duplicate]

Tags:

r

na

I am trying to find a simple way of counting the non missing cases in a column of a data frame. I have used the function:

foo<- function(x) { sum(!is.na(x)) }

and then apply it to a data frame via sapply()

stats$count <- sapply(OldExaminee, foo2, simplify=T)

Although this is working fine, I am just in disbelieve that there isn't a simpler way of counting, i.e. something in the base set of function.

Any ideas?

like image 680
SprengMeister Avatar asked Mar 29 '13 13:03

SprengMeister


People also ask

How do you count the number of NA in a column?

The easiest way to count the number of NA's in R in a single column is by using the functions sum() and is.na(). The is.na() function takes one column as input and converts all the missing values into ones and all other values into zeros.

Which aggregate function counts the number of non NA values in the group?

Which aggregate function counts the number of non NA values in the group? The SAS function N calculates the number of non-blank numeric values across multiple columns.

How do I find missing values in a column in R?

In R, missing values are represented by the symbol NA (not available). Impossible values (e.g., dividing by zero) are represented by the symbol NaN (not a number). Unlike SAS, R uses the same symbol for character and numeric data.

How do I check for NA values in R?

To check which value in NA in an R data frame, we can use apply function along with is.na function. This will return the data frame in logical form with TRUE and FALSE.


1 Answers

For a data.frame you can get it using colSums and is.na:

set.seed(45)
df <- data.frame(matrix(sample(c(NA,1:5), 50, replace=TRUE), ncol=5))
#    X1 X2 X3 X4 X5
# 1   3  2 NA  2 NA
# 2   1  5  1  1  4
# 3   1  1  3  2  3
# 4   2  2  3  5  3
# 5   2  2  5  2  2
# 6   1  2 NA  3  3
# 7   1  5  5  5  2
# 8   3 NA  4  1  5
# 9   1  2  3 NA  1
# 10 NA  1  1  2  2

colSums(!is.na(df))
# X1 X2 X3 X4 X5 
#  9  9  8  9  9 
like image 167
Arun Avatar answered Oct 28 '22 16:10

Arun