[英]Pivot all columns wider (except ID columns) using pivot_wider() in R/tidyverse
我正在嘗試在R
數據框從長數據框轉換為寬數據框。 我正在嘗試使用pivot_wider()
。 這是一個最小的工作示例:
library("tidyr")
set.seed(12345)
sampleSize <- 10
timepoints <- 3
raters <- 2
data_long <- data.frame(ID = rep(1:sampleSize, each = timepoints * raters),
time = rep(1:timepoints, times = sampleSize * raters),
rater = rep(c("a","b"), times = sampleSize * timepoints),
v1 = sample.int(99, sampleSize * timepoints * raters, replace = TRUE),
v2 = sample.int(99, sampleSize * timepoints * raters, replace = TRUE),
v3 = sample.int(99, sampleSize * timepoints * raters, replace = TRUE),
v100 = sample.int(99, sampleSize * timepoints * raters, replace = TRUE),
vA = sample.int(99, sampleSize * timepoints * raters, replace = TRUE),
vB = sample.int(99, sampleSize * timepoints * raters, replace = TRUE),
vC = sample.int(99, sampleSize * timepoints * raters, replace = TRUE),
vZZ = sample.int(99, sampleSize * timepoints * raters, replace = TRUE))
以下是數據:
> tibble(data_long)
# A tibble: 60 x 11
ID time rater v1 v2 v3 v100 vA vB vC vZZ
<int> <int> <chr> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 1 a 14 56 30 75 66 22 8 73
2 1 1 b 90 44 99 8 36 72 1 78
3 1 2 a 92 35 93 46 4 68 39 52
4 1 2 b 51 91 50 67 43 72 99 74
5 1 3 a 80 34 31 31 21 52 7 23
6 1 3 b 24 86 25 86 20 43 74 89
7 2 1 a 58 51 48 60 6 56 66 37
8 2 1 b 96 95 76 1 78 2 65 3
9 2 2 a 88 26 92 86 7 37 84 15
10 2 2 b 93 55 25 62 27 39 73 85
# ... with 50 more rows
在這個例子中,我有三個唯一標識所有觀察值的列: ID
、 time
和rater
。 我想按rater
擴大所有其他列(即,不包括ID
和time
列)。 我預期的 output 是:
# A tibble: 30 x 18
ID time v1_a v1_b v2_a v2_b v3_a v3_b v100_a v100_b vA_a vA_b vB_a vB_b vC_a vC_b vZZ_a vZZ_b
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 1 14 90 56 44 30 99 75 8 66 36 22 72 8 1 73 78
2 1 2 92 51 35 91 93 50 46 67 4 43 68 72 39 99 52 74
3 1 3 80 24 34 86 31 25 31 86 21 20 52 43 7 74 23 89
4 2 1 58 96 51 95 48 76 60 1 6 78 56 2 66 65 37 3
5 2 2 88 93 26 55 92 25 86 62 7 27 37 39 84 73 15 85
6 2 3 75 2 23 55 28 8 66 74 65 92 58 10 91 65 7 44
7 3 1 86 94 7 87 78 85 38 87 36 49 89 83 33 34 32 38
8 3 2 10 75 12 15 21 18 56 77 54 17 61 92 18 50 98 27
9 3 3 38 81 46 90 20 47 88 15 33 95 66 19 12 27 84 52
10 4 1 32 38 88 68 77 71 10 81 21 54 33 16 90 41 29 72
# ... with 20 more rows
我可以使用以下語法加寬任何給定的列:
data_long %>%
pivot_wider(names_from = rater, values_from = c(v1, v2))
因此,我可以通過在向量中手動輸入所有列來加寬所有列:
data_long %>%
pivot_wider(names_from = rater, values_from = c(v1, v2, v3, v100, vA, vB, vC, vZZ))
但是,如果我有很多列,這會變得很笨拙。 另一種方法是通過指定列的范圍來加寬列:
data_long %>%
pivot_wider(names_from = rater, values_from = v1:vZZ)
但是,如果要加寬的所有列不在一個范圍內,例如如果 ID 列散布在整個數據框中(盡管可以指定多個范圍),則此方法效果不佳。
有沒有一種方法可以使用pivot_wider()
來加寬所有列,除了我指定為使用id_cols
(即ID
和time
)唯一標識每個觀察值的列的任何列。 我希望解決方案可以擴展到我有很多列的情況(因此不想指定變量名稱或要擴大的變量范圍)。
正如我們所知,前 3 列應該是固定的,請在values_from
中的那些列名上使用-
library(dplyr)
library(tidyr)
data_long %>%
pivot_wider(names_from = rater, values_from = -names(.)[1:3])
或者如果我們已經創建了一個 object
id_cols <- c("ID", "time")
data_long %>%
pivot_wider(names_from = rater, values_from = -all_of(id_cols))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.