2

我试图让 expss use_labels 与 dplyr 逻辑一起使用 - 请参见下面的示例。

小插图在 use_labels 下声明了以下内容。到目前为止,变量标签仅支持将在 data.frame 中评估的表达式。这是我在这里遇到的问题吗?

#
##########################################
library(expss)
library(tidyverse)
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)

# table with caption from label - labels working
cro_cpct(mtcars$am, mtcars$vs) %>% set_caption(var_lab(mtcars$am))

## This works as expected - now to get this with expss use_labels.
mtcars %>%
 group_by(am) %>%
  summarise(
   freq = n()
)
#######
#am             freq
#<labelled>    <int>
# 1 0             19
# 2 1             13
########################

#### This doesn't work - i.e. not labelled
use_labels(mtcars %>%
  group_by(am) %>%
   summarise(
    freq = n()
))
## Error in substitute_symbols(expr, c(substitution_list, list(..data = quote(expss::vars(other))))) : 
  # argument "expr" is missing, with no default

如果标签不能与 dplyr 逻辑一起使用,有人知道另一个可以用 dplyr 做标签的包吗?问候

4

2 回答 2

2

您可以使用该...data参数访问表达式中的数据,并values2labels(感谢@Gregory Demin)获取标签。

library(expss)  
use_labels(mtcars, ..data %>% 
                      group_by(am) %>% 
                      summarise(freq = n()) %>% values2labels)
# A tibble: 2 x 2
#  Transmission  freq
#  <labelled>   <int>
#1 Automatic       19
#2 Manual          13
于 2019-12-06T07:08:18.250 回答
0

外国包裹确实保留了价值标签。Haven 没有,或者至少默认情况下没有,我不知道如何实现它。str()您可以通过检查导入的数据来确认这一点。如果您在某处看不到标签,则它们没有被导入。在这里,我正在导入一个带有 union 和 female 的值标签的 Stata 数据集,它们都是 0/1 底层。当使用包 foreign ( )导入时,该cro_cpct命令使用值标签,但不使用变量标签。read.dta使用包 Haven ( read_dta) 导入时,cro_cpct使用变量标签而不是值标签。

    library(expss)
    library(tidyverse)
    library(foreign)
    wages <- read.dta("c:/users/pjargowsky/documents/course/qm2/wages.dta")
    cro_cpct(wages$female, wages$union) %>% set_caption(var_lab(wages$female))

#
# |              |              | wages$union |      |
# |              |              |          no |  yes |
# | ------------ | ------------ | ----------- | ---- |
# | wages$female |         male |        50.5 | 70.8 |
# |              |       female |        49.5 | 29.2 |
# |              | #Total cases |       438.0 | 96.0 |

library(haven)
wages <- read_dta("c:/users/pjargowsky/documents/course/qm2/wages.dta")
cro_cpct(wages$female, wages$union) %>% set_caption(var_lab(wages$female))

# Gender                                                    
# |        |              | Union Membership |      |
# |        |              |                0 |    1 |
# | ------ | ------------ | ---------------- | ---- |
# | Gender |            0 |             50.5 | 70.8 |
# |        |            1 |             49.5 | 29.2 |
# |        | #Total cases |            438.0 | 96.0 |
于 2020-05-28T22:20:21.097 回答