Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting non NAs in a data frame; getting answer as a vector

Tags:

r

na

Say I have the following R data.frame ZZZ:

( ZZZ <- structure(list(n = c(1, 2, NA), m = c(6, NA, NA), o = c(7, 8,  8)), .Names = c("n", "m", "o"), row.names = c(NA, -3L), class = "data.frame") )  ## not run    n  m o 1  1  6 7 2  2 NA 8 3 NA NA 8 

I want to know, in the form of a vector, how many non-NAs I've got. I want the answer available to me as:

2, 1, 3 

When I use the command length(ZZZ), I get 3, which of course is the number of vectors in the data.frame, a valuable enough piece of information.

I have other functions that operate on this data.frame and give me answers in the form of vectors, but, dang-it, length doesn't operate like that.

like image 952
Plsvn Avatar asked Feb 13 '11 18:02

Plsvn


People also ask

Can you have a vector of Dataframes?

A data frame can be stored numeric data or character data or factor type data. Each column in the data frame should contain an equal number of the data elements. The Data frame can be converted from vectors in R. To create a data frame in R using the vector, we must first have a series of vectors containing data.

Is a data frame a vector?

Data Frames A data frame is a tabular data structure, consisting of rows and columns and implemented as a list. The columns of a data frame can consist of different data types but each column must be a single data type [like a vector].

How do I count the number of NA values in a column in R?

The is.na() function takes one column as input and converts all the missing values into ones and all other values into zeros. Then, using the sum() function, one can sum all the ones and thus count the number of NA's in a column.


1 Answers

colSums(!is.na(x)) 

Vectorisation ftw.

like image 123
hadley Avatar answered Oct 08 '22 21:10

hadley