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.
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.
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].
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.
colSums(!is.na(x))
Vectorisation ftw.
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