如何在数据帧上使用pivot_longer()来包含一组特定的列?

如何在数据帧上使用pivot_longer()来包含一组特定的列?,r,R,我有一个数据帧,df: df <- structure(list(estrogen = c(-2, 2, 0, 1, -3, 1, -1, 1), testosterone = c(-1.84, 1.14, -0.35, 0.68, -2.04, 1.08, -0.48, 0.58), growth_hormone = c(-1.81, 0.98, -0.42, 0.58, -0.73, 2.05, 0.66, 0.38), insulin = c(-1.58, 1.37, -0.1

我有一个数据帧,
df

df <- structure(list(estrogen = c(-2, 2, 0, 1, -3, 1, -1, 1), testosterone = c(-1.84, 
1.14, -0.35, 0.68, -2.04, 1.08, -0.48, 0.58), growth_hormone = c(-1.81, 
0.98, -0.42, 0.58, -0.73, 2.05, 0.66, 0.38), insulin = c(-1.58, 
1.37, -0.11, 0.89, -2.76, 0.39, -1.18, 0.17), treatment = c("2mg", 
"2mg", "2mg", "2mg", "2mg", "2mg", "2mg", "2mg"), time = c("V3", 
"V3", "V3", "V3", "V4", "V4", "V4", "V4"), type = c("lci", "uci", 
"Estimate", "P", "lci", "uci", "Estimate", "P")), row.names = c(NA, 
8L), class = "data.frame")



> df
  estrogen testosterone growth_hormone insulin treatment time     type
1       -2        -1.84          -1.81   -1.58       2mg   V3      lci
2        2         1.14           0.98    1.37       2mg   V3      uci
3        0        -0.35          -0.42   -0.11       2mg   V3 Estimate
4        1         0.68           0.58    0.89       2mg   V3        P
5       -3        -2.04          -0.73   -2.76       2mg   V4      lci
6        1         1.08           2.05    0.39       2mg   V4      uci
7       -1        -0.48           0.66   -1.18       2mg   V4 Estimate
8        1         0.58           0.38    0.17       2mg   V4        P

如果ID列的名称没有任何定义功能(例如,因此我不能使用“contains”或类似的名称?

我建议下一种方法。您可以在
pivot\u longer()
中定义变量以保持为
cols
,然后对列排序并排列行:

library(tidyverse)
#Data
df %>% pivot_longer(cols = -c(treatment,time,type)) %>%
  select(name,value,treatment,time,type) %>% arrange(name)
输出:

             name value treatment time     type
1        estrogen -2.00       2mg   V3      lci
2        estrogen  2.00       2mg   V3      uci
3        estrogen  0.00       2mg   V3 Estimate
4        estrogen  1.00       2mg   V3        P
5        estrogen -3.00       2mg   V4      lci
6        estrogen  1.00       2mg   V4      uci
7        estrogen -1.00       2mg   V4 Estimate
8        estrogen  1.00       2mg   V4        P
9  growth_hormone -1.81       2mg   V3      lci
10 growth_hormone  0.98       2mg   V3      uci
11 growth_hormone -0.42       2mg   V3 Estimate
12 growth_hormone  0.58       2mg   V3        P
13 growth_hormone -0.73       2mg   V4      lci
14 growth_hormone  2.05       2mg   V4      uci
15 growth_hormone  0.66       2mg   V4 Estimate
16 growth_hormone  0.38       2mg   V4        P
17        insulin -1.58       2mg   V3      lci
18        insulin  1.37       2mg   V3      uci
19        insulin -0.11       2mg   V3 Estimate
20        insulin  0.89       2mg   V3        P
21        insulin -2.76       2mg   V4      lci
22        insulin  0.39       2mg   V4      uci
23        insulin -1.18       2mg   V4 Estimate
24        insulin  0.17       2mg   V4        P
25   testosterone -1.84       2mg   V3      lci
26   testosterone  1.14       2mg   V3      uci
27   testosterone -0.35       2mg   V3 Estimate
28   testosterone  0.68       2mg   V3        P
29   testosterone -2.04       2mg   V4      lci
30   testosterone  1.08       2mg   V4      uci
31   testosterone -0.48       2mg   V4 Estimate
32   testosterone  0.58       2mg   V4        P

另一种方法是使用
gather
from
tidyr
package

library(tidyr)
library(dplyr)
df %>% gather("ID", "value", -c(treatment,time,type))
#    treatment time     type             ID value
# 1        2mg   V3      lci       estrogen -2.00
# 2        2mg   V3      uci       estrogen  2.00
# 3        2mg   V3 Estimate       estrogen  0.00
# 4        2mg   V3        P       estrogen  1.00
# 5        2mg   V4      lci       estrogen -3.00
# 6        2mg   V4      uci       estrogen  1.00
# 7        2mg   V4 Estimate       estrogen -1.00
# 8        2mg   V4        P       estrogen  1.00
# 9        2mg   V3      lci   testosterone -1.84
# 10       2mg   V3      uci   testosterone  1.14
# 11       2mg   V3 Estimate   testosterone -0.35
# 12       2mg   V3        P   testosterone  0.68
# 13       2mg   V4      lci   testosterone -2.04
# 14       2mg   V4      uci   testosterone  1.08
# 15       2mg   V4 Estimate   testosterone -0.48
# 16       2mg   V4        P   testosterone  0.58
# 17       2mg   V3      lci growth_hormone -1.81
# 18       2mg   V3      uci growth_hormone  0.98
# 19       2mg   V3 Estimate growth_hormone -0.42
# 20       2mg   V3        P growth_hormone  0.58
# 21       2mg   V4      lci growth_hormone -0.73
# 22       2mg   V4      uci growth_hormone  2.05
# 23       2mg   V4 Estimate growth_hormone  0.66
# 24       2mg   V4        P growth_hormone  0.38
# 25       2mg   V3      lci        insulin -1.58
# 26       2mg   V3      uci        insulin  1.37
# 27       2mg   V3 Estimate        insulin -0.11
# 28       2mg   V3        P        insulin  0.89
# 29       2mg   V4      lci        insulin -2.76
# 30       2mg   V4      uci        insulin  0.39
# 31       2mg   V4 Estimate        insulin -1.18
# 32       2mg   V4        P        insulin  0.17
library(tidyr)
library(dplyr)
df %>% gather("ID", "value", -c(treatment,time,type))
#    treatment time     type             ID value
# 1        2mg   V3      lci       estrogen -2.00
# 2        2mg   V3      uci       estrogen  2.00
# 3        2mg   V3 Estimate       estrogen  0.00
# 4        2mg   V3        P       estrogen  1.00
# 5        2mg   V4      lci       estrogen -3.00
# 6        2mg   V4      uci       estrogen  1.00
# 7        2mg   V4 Estimate       estrogen -1.00
# 8        2mg   V4        P       estrogen  1.00
# 9        2mg   V3      lci   testosterone -1.84
# 10       2mg   V3      uci   testosterone  1.14
# 11       2mg   V3 Estimate   testosterone -0.35
# 12       2mg   V3        P   testosterone  0.68
# 13       2mg   V4      lci   testosterone -2.04
# 14       2mg   V4      uci   testosterone  1.08
# 15       2mg   V4 Estimate   testosterone -0.48
# 16       2mg   V4        P   testosterone  0.58
# 17       2mg   V3      lci growth_hormone -1.81
# 18       2mg   V3      uci growth_hormone  0.98
# 19       2mg   V3 Estimate growth_hormone -0.42
# 20       2mg   V3        P growth_hormone  0.58
# 21       2mg   V4      lci growth_hormone -0.73
# 22       2mg   V4      uci growth_hormone  2.05
# 23       2mg   V4 Estimate growth_hormone  0.66
# 24       2mg   V4        P growth_hormone  0.38
# 25       2mg   V3      lci        insulin -1.58
# 26       2mg   V3      uci        insulin  1.37
# 27       2mg   V3 Estimate        insulin -0.11
# 28       2mg   V3        P        insulin  0.89
# 29       2mg   V4      lci        insulin -2.76
# 30       2mg   V4      uci        insulin  0.39
# 31       2mg   V4 Estimate        insulin -1.18
# 32       2mg   V4        P        insulin  0.17