简体   繁体   中英

How to deal with NA values with different labels

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!

Check this:

base::table(haven::as_factor(x, levels = "labels"))

output

Agreement Disagreement        First 
       2            1            1 
 Refused     Not home 
       1            1 

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM