Reputation: 95
I am having a problem dealing with NA values from a survey dataset.
library(haven)
x <- labelled(
c(1:3, tagged_na("a", "c", "z"), 4:1),
c("Agreement" = 1, "Disagreement" = 4,
"First" = tagged_na("c"),
"Refused" = tagged_na("a"),
"Not home" = tagged_na("z"))
)
Here is the output
<Labelled double>
[1] 1 2 3 NA(a) NA(c) NA(z) 4 3 2 1
Labels:
value label
1 Agreement
4 Disagreement
NA(c) First
NA(a) Refused
NA(z) Not home
What I want is to calculate how many NA(c), NA(a), and NA(z) individually, not as a whole. And I would like to subset data based on NA(c), NA(a), and NA(z). How can I do this.
Thanks!
Upvotes: 0
Views: 765
Reputation: 388982
We could use get_values
function from sjlabelled
package, extract value which have NA
in them and use table
to get their count.
table(grep('NA', sjlabelled::get_values(x), value = TRUE))
# NA(a) NA(c) NA(z)
# 1 1 1
Upvotes: 0
Reputation: 1076
Check this:
base::table(haven::as_factor(x, levels = "labels"))
output
Agreement Disagreement First
2 1 1
Refused Not home
1 1
Upvotes: 1