首页 > 解决方案 > 如何在数据帧上使用 pivot_longer() 来包含一组特定的列?

问题描述

我有一个数据框,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

如何使用 pivot_longer 创建一个新列 ( ID),它将列出所有列值,如下所示:

ID         value  treatment   time     type
estrogen    -2          2mg    V3      lci
estrogen     2          2mg    V3      uci
estrogen     0          2mg    V3      Estimate
estrogen     1          2mg    V3      P
estrogen    -3          2mg    V4      lci

etc..
insulin   0.17          2mg    V4      P

当 ID 列的名称没有任何定义特征时,我该怎么做(即我不能使用“包含”或类似的东西?

标签: r

解决方案


我会建议下一种方法。您可以定义要保留的变量colspivot_longer()然后对列进行排序并排列行:

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

推荐阅读