R expss use_labels 和 dplyr 逻辑
Posted
技术标签:
【中文标题】R expss use_labels 和 dplyr 逻辑【英文标题】:R expss use_labels and dplyr logic 【发布时间】:2020-03-31 03:37:05 【问题描述】:我试图让 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 做标签的包吗? 问候
【问题讨论】:
【参考方案1】:您可以使用...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
【讨论】:
感谢您,但 tibble 中没有标签。标签类似于自动和手动 啊……我明白了。抱歉,我找不到这样做的方法。我稍后会尝试检查。 @MarkWebbuse_labels
用于变量标签。在summarise
中,值标签被保留但未显示在结果中。为了向他们展示使用values2labels
作为链中的最后一个操作,就在summary 之后。
@GregoryDemin 谢谢,这对我来说是新的。您介意我在答案中更新该部分吗?还是您想将其作为单独的答案发布?
@RonakShah 你最好更新你的答案。两个几乎相同的帖子没有理由。【参考方案2】:
Package foreign 确实保留了值标签。 Haven 没有,或者至少默认情况下没有,我不知道如何实现它。您可以通过检查导入数据的str()
来确认这一点。如果您在某处看不到标签,则它们没有被导入。在这里,我正在导入一个带有 union 和 female 的值标签的 Stata 数据集,它们都是 0/1 底层。当使用外部包 (read.dta
) 导入时,cro_cpct
命令使用值标签,但不使用变量标签。当使用包 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 |
【讨论】:
可以在导入之后用haven试试wages = add_labelled_class(wages)
吗?它会起作用吗?
格雷格,这对我不起作用。我需要 read_dta() 上的选项吗?
你能从 clean session 开始,然后在have包之后加载 expss 吗?之后再试一次wages = add_labelled_class(wages)
。如果不行,请提供dput(wages[1:10, c("female","union")])
的结果。
格雷格,我想我明白了。作为我的目标的 Stata 数据集非常古老(2002 年)。谁知道那是什么版本的Stata。多年来我一直在教学中使用。我将其重新保存为 Stata 16 数据集。现在 package foreign 甚至都不会读取它,但是 package have 会,并且包含并使用了价值标签。感谢您的帮助。以上是关于R expss use_labels 和 dplyr 逻辑的主要内容,如果未能解决你的问题,请参考以下文章
如何防止 R Expss 在输出数据框中将变量名称与行标签混合?
使用 R expss 和 data.table 是不是可以从 csv 文件加载 data.table 标签,而不是手动输入代码?