有没有办法对srvyr中生成的测量设计对象使用pivot_longer()?我想做一个显示一系列变量的多面图,所以为了做到这一点,我想把四到五个人口统计变量放入一个很长的数据框架中,并绘制加权数据。
下面的代码是可重现的,但确实安装了一个包。很抱歉,这是我能找到的最好的重现问题的方法。使用未加权的数据很容易获得所需的内容,但我不知道如何使用加权的数据。
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()发布于 2021-05-08 03:16:45
没有计划在srvyr中实现pivot_longer或pivot_wider (或其他更改数据集维度的tidyr函数),因为它们更改数据集的方式需要更改无法自动确定的调查设计。您需要知道如何为新数据集指定正确的设计。
我一直认为,在将数据集转换为tbl_svy之前,应该先对其进行透视(或整理)。
https://stackoverflow.com/questions/67437570
复制相似问题