带有dplyr的标签名称的摘要

时间:2018-07-14 06:01:36

标签: r dplyr r-haven

我已经用Haven导入了一个.sav文件,但我遇到的困难是我似乎无法弄清楚如何在原地或使用标签编码打印标签名称。标签: 1 =待业, 2 =外观

Employment <- select(well_being_df, EmploymentStatus, Gender) %>% <group_by(EmploymentStatus) %>% summarise_all(funs(mean, n = n(), sd,min(.,is.na = TRUE), max(.,is.na = TRUE)))


# A tibble: 5 x 6
  EmploymentStatus  mean     n    sd   min   max
  <dbl+lbl>        <dbl> <int> <dbl> <dbl> <dbl>
 1 1                 1.67    12 0.492     1     2
 2 2                 1.17     6 0.408     1     2
 3 3                 1.8     85 0.431     1     3
 4 4                 1.5     62 0.504     1     2
 5 5                 1.5      4 0.577     1     2

理想情况:

 # A tibble: 5 x 6
EmploymentStatus  mean     n    sd   min   max
<dbl+lbl>        <dbl> <int> <dbl> <dbl> <dbl>
1 1  Unemployed     1.67    12 0.492     1     2
2 2  Looking        1.17     6 0.408     1     2
3 3  Etc            1.8     85 0.431     1     3
4 4                 1.5     62 0.504     1     2
5 5                 1.5      4 0.577     1     2

dput(head(well_being_df, 10))
structure(list(Age = c(22, 20, 23, 20, 25, 18, 24, 21, 21, 30.7344197070233
), Gender = structure(c(2, 2, 1, 2, 1, 2, 2, 2, 2, 1), labels = c(Male = 1, 
Female = 2, Transgender = 3), class = "labelled"), EmploymentStatus = structure(c(3, 
1, 4, 3, 3, 3, 3, 4, 3, 4), labels = c(`Unemployed but not looking` = 1, 
`Unemployed and looking` = 2, `Part-time` = 3, `Full-time` = 4, 
Retired = 5), class = "labelled"), Cognition1 = structure(c(6, 
3, 6, 5, 9, 6, 4, 4, 7, 5), labels = c(`Provides nothing that you want` = 0, 
`Provides half of what you want` = 5, `Provides all that you want` = 10
), class = "labelled"), Cognition2 = structure(c(7, 3, 8, 
5, 8, 5, 5, 7, 7, 3), labels = c(`Far below average` = 0, 
`About Average` = 5, `Far above average` = 10), class = "labelled"), 
Cognition3 = structure(c(6, 5, 4, 5, 6, 5, 5, 5, 5, 5), labels = c(`Far less than you deserve` = 0, 
`About what you deserve` = 5, `Far more than you deserve` = 10
), class = "labelled"), Cognition4 = structure(c(7, 3, 6, 
2, 8, 3, 3, 5, 6, 2), labels = c(`Far less than you need` = 0, 
`About what you need` = 5, `Far more than you need` = 10), class = "labelled"), 
Cognition5 = structure(c(10, 9, 6, 3, 7, 2, 2, 0, 4, 0), labels = c(`Far less than expected` = 0, 
`About as expected` = 5, `Far more than expected` = 10), class = "labelled"), 
Cognition6 = structure(c(8, 6, 0, 3, 3, 8, 9, 10, 5, 10), labels = c(`Far more than it will in the future` = 0, 
`About what you expect in the future` = 5, `Far less than what the future will offer` = 10
), class = "labelled"), Cognition7 = structure(c(9, 7, 10, 
5, 6, 2, 3, 0, 8, 3), labels = c(`Far below previous best` = 0, 
`Equals previous best` = 5, `Far above previous best` = 10
), class = "labelled")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

1 个答案:

答案 0 :(得分:1)

Employment <- select(well_being_df, EmploymentStatus, Gender) %>% 
    mutate(EmploymentStatus = labelled::to_factor(EmploymentStatus)) %>% # use labelled package 
    group_by(EmploymentStatus) %>% 
    summarise_all(funs(mean, n = n(), sd,min(.,is.na = TRUE), max(.,is.na = TRUE)))