這個問題與總結調查結果有關
這種情況是同一問題有多個變體,所以
df <- data.frame(
survey_question_subquestionA = c("Less","More","Less","More","Missing","More",Less")
survey_question_subquestionB = c("Less","More","Less","More","Missing","More",Less")
survey_question_subquestionC = c("Less","More","Less","More","Missing","More",Less")
survey_weights = c( 1.02, 1.05, 0.97, 0.92, 0.94, 1.03, 0.98 )
目標是最終得到一張桌子
| 更多的 | 較少的 | 失蹤 | |
|---|---|---|---|
| 子問題A | n | n | n |
| 子問題B | n | n | n |
| 子問題C | n | n | n |
| 全部的 | n | n | n |
qn <- "Question"
wt <- "Survey_Weights"
tvdf |> filter(Question == qn) |> select(Variable) |> unlist() |> unname() -> CVL
在現實生活中,我從另一個包含稱為 tvdf 的元資訊的資料框中提取變數名稱,并且資料框本身包含許多其他問題和調查。
這適用于未加權計數
df |> select(any_of(CVL)) |> pivot_longer(cols = contains(qn), names_to = "Variable", values_to = "Values") |> group_by(Variable) |> count(Values) |> pivot_wider(names_from = Values, values_from = n) -> table
問題是
count(Values, wt = get(wt))
不起作用,因為它們在選擇階段被排除在外。如果我將它們包括在內,它們最終會出現在我的表中。
歡迎任何建議。我也對 data.table 解決方案持開放態度。
uj5u.com熱心網友回復:
這是你要找的嗎?對于“總計”行的添加,我使用了janitor具有方便功能的包adorn_totals()。
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.2.1
#> Warning: package 'tibble' was built under R version 4.2.1
#> Warning: package 'tidyr' was built under R version 4.2.1
#> Warning: package 'readr' was built under R version 4.2.1
#> Warning: package 'purrr' was built under R version 4.2.1
#> Warning: package 'dplyr' was built under R version 4.2.1
#> Warning: package 'stringr' was built under R version 4.2.1
#> Warning: package 'forcats' was built under R version 4.2.1
df <- tibble(
survey_question_subquestionA = c("Less",
"More",
"Less",
"More",
"Missing",
"More",
"Less"),
survey_question_subquestionB = c("Less", "More", "Less", "More", "Missing", "More", "Less"),
survey_question_subquestionC = c("Less",
"More",
"Less",
"More",
"Missing",
"More",
"Less"),
survey_weights = c(1.02, 1.05, 0.97, 0.92, 0.94, 1.03, 0.98)
)
df |>
pivot_longer(cols = 1:3,
names_to = "question") |>
count(question, value, wt = survey_weights) |>
pivot_wider(names_from = value,
values_from = n) |>
janitor::adorn_totals()
#> question Less Missing More
#> survey_question_subquestionA 2.97 0.94 3
#> survey_question_subquestionB 2.97 0.94 3
#> survey_question_subquestionC 2.97 0.94 3
#> Total 8.91 2.82 9
使用reprex v2.0.2創建于 2022-11-04
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/527260.html
上一篇:x值的列組,y值的列組(每行)
