我可以将 pivot_longer() 与 srvyr 的调查设计对象一起使用吗?
Can I use pivot_longer() with a survey design object from srvyr?
有没有办法将 pivot_longer()
用于 srvyr
中生成的调查设计对象?我想制作一个显示一系列变量的多面图,因此我想将四个或五个人口统计变量放入一个长数据框中并绘制加权数据。
以下代码可重现,但确实安装了一个包。抱歉,这是我能想出的最好的方法来重现我的问题。使用未加权数据很容易获得我需要的数据,但我不知道如何使用加权数据。
library(tidyverse)
library(srvyr)
#output of dput
ces2019_web<-tibble::tribble(
~cps19_province, ~cps19_education, ~cps19_weight_general_all,
"24", "10", 0.681336402893066,
"24", "10", 0.681336402893066,
"22", "8", 0.650459408760071,
"22", "8", 0.88725334405899,
"22", "5", 1.65380775928497,
"22", "8", 0.650459408760071
)
# convert values to factor type
ces2019_web <- to_factor(ces2019_web)
#Make the survey design
ces2019_web %>%
filter(!is.na(cps19_weight_general_all)) %>%
as_survey_design(., weight=cps19_weight_general_all)->des
#Look for two demographic variables similar to what I'm working with
#Show the ideal end product but for unweighted data
ces2019_web %>%
as_factor() %>%
pivot_longer(., cols=c(1:2)) %>%
group_by(name, value) %>%
summarize(n=n()) %>%
mutate(pct=n/sum(n)) %>%
knitr::kable()
#Show with weighted data;
#Require a solution
des %>%
select(cps19_province, cps19_education) %>%
pivot_longer()
没有计划在 srvyr 中实现 pivot_longer
或 pivot_wider
(或其他改变数据集维度的 tidyr 函数),因为它们以需要改变调查设计的方式改变数据集无法自动确定。您需要知道如何使用新数据集指定正确的设计。
我一直觉得在将数据集转换为 tbl_svy
.
之前,您应该旋转(或整理)数据集
有没有办法将 pivot_longer()
用于 srvyr
中生成的调查设计对象?我想制作一个显示一系列变量的多面图,因此我想将四个或五个人口统计变量放入一个长数据框中并绘制加权数据。
以下代码可重现,但确实安装了一个包。抱歉,这是我能想出的最好的方法来重现我的问题。使用未加权数据很容易获得我需要的数据,但我不知道如何使用加权数据。
library(tidyverse)
library(srvyr)
#output of dput
ces2019_web<-tibble::tribble(
~cps19_province, ~cps19_education, ~cps19_weight_general_all,
"24", "10", 0.681336402893066,
"24", "10", 0.681336402893066,
"22", "8", 0.650459408760071,
"22", "8", 0.88725334405899,
"22", "5", 1.65380775928497,
"22", "8", 0.650459408760071
)
# convert values to factor type
ces2019_web <- to_factor(ces2019_web)
#Make the survey design
ces2019_web %>%
filter(!is.na(cps19_weight_general_all)) %>%
as_survey_design(., weight=cps19_weight_general_all)->des
#Look for two demographic variables similar to what I'm working with
#Show the ideal end product but for unweighted data
ces2019_web %>%
as_factor() %>%
pivot_longer(., cols=c(1:2)) %>%
group_by(name, value) %>%
summarize(n=n()) %>%
mutate(pct=n/sum(n)) %>%
knitr::kable()
#Show with weighted data;
#Require a solution
des %>%
select(cps19_province, cps19_education) %>%
pivot_longer()
没有计划在 srvyr 中实现 pivot_longer
或 pivot_wider
(或其他改变数据集维度的 tidyr 函数),因为它们以需要改变调查设计的方式改变数据集无法自动确定。您需要知道如何使用新数据集指定正确的设计。
我一直觉得在将数据集转换为 tbl_svy
.