我想在 R 中收集数据框的多列以使其“整洁”。
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
预期产出
# time model value ci_low ci_high
# 1 a -0.3591146 -0.5881655 -0.4486189
# 2 a 0.8952444 ... and so on
问题
我正在努力使用包中的gather
功能tidyr
。如何正确使用它来重新组织此数据框?