-1

Below is a sample Data Frame

var_1 <- c(2,5,6,7,9)
var_2 <- c(1,"NA",3,6,1)
var_3 <- c(9,4,6,"NA","NA")
varDF <- data.frame(var_1,var_2, var_3)

How do I get the count of observations in a particular column? e.g column var_1 I'd like the output to be 5 & and var_2 to be 4 (Because it has skipped the NA)

andy
  • 1,947
  • 5
  • 27
  • 46

1 Answers1

0

Using colSums and !is.na():

colSums(!is.na(varDF))

var_1 var_2 var_3 
    5     4     3 

Or if your NAs are actually "NA", use the following, although I advise to use NAs in numeric and integer columns:

colSums(varDF != "NA")
Lennyy
  • 5,932
  • 2
  • 10
  • 23