簡體   English   中英

在 R 中收集多個列

[英]Gather multiple columns with gather in R

我想在 R 中收集 dataframe 的多列以使其“整潔”。

library(tidyverse)
set.seed(123)

df <- data.frame(time = seq(1,5,1), 
                 value_model_a = rnorm(5), 
                 ci_low_model_a = rnorm(5),
                 ci_high_model_a = rnorm(5),
                 value_model_b = rnorm(5), 
                 ci_low_model_b = rnorm(5),
                 ci_high_model_b = rnorm(5))

#  time value_model_a ci_low_model_a ci_high_model_a value_model_b ci_low_model_b ci_high_model_b
#1    1    -0.3591146     -0.5881655      -0.4486189     0.7821898     -0.5315449      0.06015936
#2    2     0.8952444     -1.6314973       0.5618802     0.8228834     -0.2663575     -0.09029613
#3    3    -1.8961105      1.1529703       0.8896495    -0.1524523      0.5989563      0.35738994
#4    4     0.3471419      0.4373451      -0.7503646     0.3670275      1.7109441      0.11028077
#5    5     1.2780844     -1.3069509      -0.1286071     1.4340957      1.1876910     -1.69710214

預期 output

# time model  value       ci_low      ci_high
# 1    a      -0.3591146  -0.5881655  -0.4486189
# 2    a      0.8952444   ... and so on

問題

我正在努力使用來自tidyr package 的gather function。 如何正確使用它來重組這個 dataframe?

一開始,旋轉可能會很困難。

新版本的gather()pivot_longer()

以下是實現預期 output 的方法。

首先,您可以將 function 告訴 pivot 一切默認情況下,僅使用時間作為您的標識符:

pivot_longer(df, -time) %>% head(5)
#> # A tibble: 30 x 3
#>     time name             value
#>    <dbl> <chr>            <dbl>
#>  1     1 value_model_a   -0.560
#>  2     1 ci_low_model_a   1.72 
#>  3     1 ci_high_model_a  1.22 
#>  4     1 value_model_b    1.79 
#>  5     1 ci_low_model_b  -1.07 

這是一個開始,但您可以通過設置名稱分隔符進一步 go。 您還可以使用names_pattern使用正則表達式。

df_l = pivot_longer(df, -time, names_sep="_model_", names_to=c("name", "model"))
df_l
#> # A tibble: 30 x 4
#>     time name    model  value
#>    <dbl> <chr>   <chr>  <dbl>
#>  1     1 value   a     -0.560
#>  2     1 ci_low  a      1.72 
#>  3     1 ci_high a      1.22 
#>  4     1 value   b      1.79 
#>  5     1 ci_low  b     -1.07 
#>  6     1 ci_high b     -1.69 
#>  7     2 value   a     -0.230
#>  8     2 ci_low  a      0.461
#>  9     2 ci_high a      0.360
#> 10     2 value   b      0.498
#> # ... with 20 more rows

最后,您期望的 output 可以通過使用具有默認值的pivot_wider()來實現(我明確地為學術目的編寫了該值):

pivot_wider(df_l, names_from = "name", values_from = "value")
#> # A tibble: 10 x 5
#>     time model   value ci_low ci_high
#>    <dbl> <chr>   <dbl>  <dbl>   <dbl>
#>  1     1 a     -0.560   1.72    1.22 
#>  2     1 b      1.79   -1.07   -1.69 
#>  3     2 a     -0.230   0.461   0.360
#>  4     2 b      0.498  -0.218   0.838
#>  5     3 a      1.56   -1.27    0.401
#>  6     3 b     -1.97   -1.03    0.153
#>  7     4 a      0.0705 -0.687   0.111
#>  8     4 b      0.701  -0.729  -1.14 
#>  9     5 a      0.129  -0.446  -0.556
#> 10     5 b     -0.473  -0.625   1.25

reprex package (v1.0.0) 於 2021 年 3 月 3 日創建

set.seed(123)

library(tidyverse)
df <- data.frame(time = seq(1,5,1), 
                 value_model_a = rnorm(5), 
                 ci_low_model_a = rnorm(5),
                 ci_high_model_a = rnorm(5),
                 value_model_b = rnorm(5), 
                 ci_low_model_b = rnorm(5),
                 ci_high_model_b = rnorm(5))

pivot_longer(df, -time, names_sep = "_model_", names_to = c(".value", "model"))
#> # A tibble: 10 x 5
#>     time model   value ci_low ci_high
#>    <dbl> <chr>   <dbl>  <dbl>   <dbl>
#>  1     1 a     -0.560   1.72    1.22 
#>  2     1 b      1.79   -1.07   -1.69 
#>  3     2 a     -0.230   0.461   0.360
#>  4     2 b      0.498  -0.218   0.838
#>  5     3 a      1.56   -1.27    0.401
#>  6     3 b     -1.97   -1.03    0.153
#>  7     4 a      0.0705 -0.687   0.111
#>  8     4 b      0.701  -0.729  -1.14 
#>  9     5 a      0.129  -0.446  -0.556
#> 10     5 b     -0.473  -0.625   1.25

reprex package (v1.0.0) 於 2021 年 3 月 3 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM