在 R 中使用均值和 CI 进行更长时间的枢轴

问题描述 投票:0回答:1

为了制作一些系数图,我想我最终需要得到一个包含 5 列的结果数据框:var、attend、mean、ci_lower 和 ci_upper。

但是,我试图

pivot_longer
旋转整个数据框的方式。我怎样才能让它只以“参加”为中心?

代码:

dtest_long <- dtest %>% 
  pivot_longer(!var, names_to = "attend", values_to = "value")

数据:

dtest <- structure(list(var = c("jbj_ext_1", "jbj_ext_2", "jbj_ext_3", 
"jbj_int_1", "jbj_int_2", "jbj_int_3", "jbj_tot_1", "jbj_tot_2", 
"jbj_tot_3"), no_attend_mean = c(11.15, 8, 7.64, 4.24, 8.26, 
7.89, 30.01, 21.08, 20.93), attend_mean = c(15.36, 10.9, 11.15, 
10.1, 4.25, 4.24, 28.98, 20.99, 19.89), no_attend_lowCI = c(9.65, 
6.5, 6.14, 2.74, 6.76, 6.39, 28.51, 19.58, 19.43), attend_lowCI = c(13.86, 
9.4, 9.65, 8.6, 2.75, 2.74, 27.48, 19.49, 18.39), no_attend_upperCI = c(12.2, 
9.05, 8.69, 5.29, 9.31, 8.94, 31.06, 22.13, 21.98), attend_upperCI = c(16.41, 
11.95, 12.2, 11.15, 5.3, 5.29, 30.03, 22.04, 20.94)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -9L))
r data-cleaning
1个回答
0
投票

感谢您澄清您的预期结果;我认为这种方法会起作用:

library(tidyverse)

dtest <- structure(list(var = c("jbj_ext_1", "jbj_ext_2", "jbj_ext_3", 
                                "jbj_int_1", "jbj_int_2", "jbj_int_3", "jbj_tot_1", "jbj_tot_2", 
                                "jbj_tot_3"), no_attend_mean = c(11.15, 8, 7.64, 4.24, 8.26, 
                                                                 7.89, 30.01, 21.08, 20.93), attend_mean = c(15.36, 10.9, 11.15, 
                                                                                                             10.1, 4.25, 4.24, 28.98, 20.99, 19.89), no_attend_lowCI = c(9.65, 
                                                                                                                                                                         6.5, 6.14, 2.74, 6.76, 6.39, 28.51, 19.58, 19.43), attend_lowCI = c(13.86, 
                                                                                                                                                                                                                                             9.4, 9.65, 8.6, 2.75, 2.74, 27.48, 19.49, 18.39), no_attend_upperCI = c(12.2, 
                                                                                                                                                                                                                                                                                                                     9.05, 8.69, 5.29, 9.31, 8.94, 31.06, 22.13, 21.98), attend_upperCI = c(16.41, 
                                                                                                                                                                                                                                                                                                                                                                                            11.95, 12.2, 11.15, 5.3, 5.29, 30.03, 22.04, 20.94)), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                            "tbl", "data.frame"), row.names = c(NA, -9L))
dtest %>%
  pivot_longer(-var,
               names_pattern = "(.*attend)_(.*)",
               names_to = c("attend", "metric")) %>%
  pivot_wider(id_cols = c("var", "attend"),
              names_from = "metric")
#> # A tibble: 18 × 5
#>    var       attend     mean lowCI upperCI
#>    <chr>     <chr>     <dbl> <dbl>   <dbl>
#>  1 jbj_ext_1 no_attend 11.2   9.65   12.2 
#>  2 jbj_ext_1 attend    15.4  13.9    16.4 
#>  3 jbj_ext_2 no_attend  8     6.5     9.05
#>  4 jbj_ext_2 attend    10.9   9.4    12.0 
#>  5 jbj_ext_3 no_attend  7.64  6.14    8.69
#>  6 jbj_ext_3 attend    11.2   9.65   12.2 
#>  7 jbj_int_1 no_attend  4.24  2.74    5.29
#>  8 jbj_int_1 attend    10.1   8.6    11.2 
#>  9 jbj_int_2 no_attend  8.26  6.76    9.31
#> 10 jbj_int_2 attend     4.25  2.75    5.3 
#> 11 jbj_int_3 no_attend  7.89  6.39    8.94
#> 12 jbj_int_3 attend     4.24  2.74    5.29
#> 13 jbj_tot_1 no_attend 30.0  28.5    31.1 
#> 14 jbj_tot_1 attend    29.0  27.5    30.0 
#> 15 jbj_tot_2 no_attend 21.1  19.6    22.1 
#> 16 jbj_tot_2 attend    21.0  19.5    22.0 
#> 17 jbj_tot_3 no_attend 20.9  19.4    22.0 
#> 18 jbj_tot_3 attend    19.9  18.4    20.9

创建于 2023-08-01,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.