I have a Data set with 201 cols and 4500 rows.
There are lots of NA's in each column. I want to get the count of the values in each columns excluding NA's.
Please help me how can I do this?
I have a Data set with 201 cols and 4500 rows.
There are lots of NA's in each column. I want to get the count of the values in each columns excluding NA's.
Please help me how can I do this?
Hi @amankumar, welcome to RStudio Community.
You can use functions from the apply
family. Here's an example.
data <- data.frame(col1 = c(4, NA, 20, 13, NA),
col2 = c(NA, 3, NA, NA, 4),
col3 = c(2, 21, NA, 5, 15))
sapply(data, function(x) sum(!is.na(x)))
#> col1 col2 col3
#> 3 2 4
Created on 2020-05-27 by the reprex package (v0.3.0)
Thank you, Its working.
I'd have used colSums
:
colSums(x = !is.na(x = data))
At least one justification (from ?colSums
):
These functions are equivalent to use of
apply
withFUN = mean
orFUN = sum
with appropriate margins, but are a lot faster.
Indeed, that is more efficient than my solution.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.