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?
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? The SAS function N calculates the number of non-blank numeric values across multiple columns.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With