将多列变成一行:按行旋转更宽
Turn multiple columns into a single row: Pivot wider by row
我一直在尝试获取包含 25 个单词的所有组合的一行,但我需要一些帮助才能使它们按正确的顺序排列。
到目前为止,我的 table 看起来像这样,其中每个单词在 Var1
中连续重复 25 次,并且 Var2
与另一个匹配 Var1
24个字。
Var1 Var2 id
laugh area 1
laugh game 2
laugh river 3
...
area laugh 25
area game 26
area river 27
...
game laugh 49
game area 50
game river 51
...
我需要将它转换为一行,值从 Var1 和 Var2 交替(因此按行组合),如下所示:
V1 V2 V3 V4 V5 V6 V25 V26 V27 V28 V29 V30
laugh area laugh game laugh river ... area laugh area game area river ...
我试过使用:
words %>% pivot_wider(names_from = id, values_from = c(Var1, Var2)
但是它按列而不是按行给我单词,即
Var2_1 Var2_2 Var2_3 Var2_25 Var2_26 Var2_27 Var1_1 Var1_2 Var1_3
laugh laugh laugh ... area area area ... area game river ...
我尝试将 names_vary = "slowest"
添加到 pivot_wider()
,但出现错误:
Error: 1 components of `...` were not used.
We detected these problematic arguments:
* `names_vary`
Did you misspecify an argument?
Backtrace:
9. tidyr::pivot_wider(...)
11. ellipsis:::action_dots(...)
library(tidyr)
library(dplyr)
billboard %>% # Replace with words
select(wk1:wk2) %>% # Replace with Var1:Var2
pivot_longer(everything()) %>%
mutate(
id = paste0('V', 1:n())
) %>%
select(-name) %>%
pivot_wider(values_from = value, names_from = id)
# A tibble: 1 x 634
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 87 82 91 87 81 70 76 76 57 34 51 39 97 97 84 62 59 53 76 76 84 84 57 47 50 39
# ... with 608 more variables: V27 <dbl>, V28 <dbl>, V29 <dbl>, V30 <dbl>, V31 <dbl>, V32 <dbl>, V33 <dbl>, V34 <dbl>, V35 <dbl>, V36 <dbl>, V37 <dbl>,
# V38 <dbl>, V39 <dbl>, V40 <dbl>, V41 <dbl>, V42 <dbl>, V43 <dbl>, V44 <dbl>, V45 <dbl>, V46 <dbl>, V47 <dbl>, V48 <dbl>, V49 <dbl>, V50 <dbl>, V51 <dbl>,
# V52 <dbl>, V53 <dbl>, V54 <dbl>, V55 <dbl>, V56 <dbl>, V57 <dbl>, V58 <dbl>, V59 <dbl>, V60 <dbl>, V61 <dbl>, V62 <dbl>, V63 <dbl>, V64 <dbl>, V65 <dbl>,
# V66 <dbl>, V67 <dbl>, V68 <dbl>, V69 <dbl>, V70 <dbl>, V71 <dbl>, V72 <dbl>, V73 <dbl>, V74 <dbl>, V75 <dbl>, V76 <dbl>, V77 <dbl>, V78 <dbl>, V79 <dbl>,
# V80 <dbl>, V81 <dbl>, V82 <dbl>, V83 <dbl>, V84 <dbl>, V85 <dbl>, V86 <dbl>, V87 <dbl>, V88 <dbl>, V89 <dbl>, V90 <dbl>, V91 <dbl>, V92 <dbl>, V93 <dbl>,
# V94 <dbl>, V95 <dbl>, V96 <dbl>, V97 <dbl>, V98 <dbl>, V99 <dbl>, V100 <dbl>, V101 <dbl>, V102 <dbl>, V103 <dbl>, V104 <dbl>, V105 <dbl>, V106 <dbl>,
# V107 <dbl>, V108 <dbl>, V109 <dbl>, V110 <dbl>, V111 <dbl>, V112 <dbl>, V113 <dbl>, V114 <dbl>, V115 <dbl>, V116 <dbl>, V117 <dbl>, V118 <dbl>, ...
我一直在尝试获取包含 25 个单词的所有组合的一行,但我需要一些帮助才能使它们按正确的顺序排列。
到目前为止,我的 table 看起来像这样,其中每个单词在 Var1
中连续重复 25 次,并且 Var2
与另一个匹配 Var1
24个字。
Var1 Var2 id
laugh area 1
laugh game 2
laugh river 3
...
area laugh 25
area game 26
area river 27
...
game laugh 49
game area 50
game river 51
...
我需要将它转换为一行,值从 Var1 和 Var2 交替(因此按行组合),如下所示:
V1 V2 V3 V4 V5 V6 V25 V26 V27 V28 V29 V30
laugh area laugh game laugh river ... area laugh area game area river ...
我试过使用:
words %>% pivot_wider(names_from = id, values_from = c(Var1, Var2)
但是它按列而不是按行给我单词,即
Var2_1 Var2_2 Var2_3 Var2_25 Var2_26 Var2_27 Var1_1 Var1_2 Var1_3
laugh laugh laugh ... area area area ... area game river ...
我尝试将 names_vary = "slowest"
添加到 pivot_wider()
,但出现错误:
Error: 1 components of `...` were not used.
We detected these problematic arguments:
* `names_vary`
Did you misspecify an argument?
Backtrace:
9. tidyr::pivot_wider(...)
11. ellipsis:::action_dots(...)
library(tidyr)
library(dplyr)
billboard %>% # Replace with words
select(wk1:wk2) %>% # Replace with Var1:Var2
pivot_longer(everything()) %>%
mutate(
id = paste0('V', 1:n())
) %>%
select(-name) %>%
pivot_wider(values_from = value, names_from = id)
# A tibble: 1 x 634
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 87 82 91 87 81 70 76 76 57 34 51 39 97 97 84 62 59 53 76 76 84 84 57 47 50 39
# ... with 608 more variables: V27 <dbl>, V28 <dbl>, V29 <dbl>, V30 <dbl>, V31 <dbl>, V32 <dbl>, V33 <dbl>, V34 <dbl>, V35 <dbl>, V36 <dbl>, V37 <dbl>,
# V38 <dbl>, V39 <dbl>, V40 <dbl>, V41 <dbl>, V42 <dbl>, V43 <dbl>, V44 <dbl>, V45 <dbl>, V46 <dbl>, V47 <dbl>, V48 <dbl>, V49 <dbl>, V50 <dbl>, V51 <dbl>,
# V52 <dbl>, V53 <dbl>, V54 <dbl>, V55 <dbl>, V56 <dbl>, V57 <dbl>, V58 <dbl>, V59 <dbl>, V60 <dbl>, V61 <dbl>, V62 <dbl>, V63 <dbl>, V64 <dbl>, V65 <dbl>,
# V66 <dbl>, V67 <dbl>, V68 <dbl>, V69 <dbl>, V70 <dbl>, V71 <dbl>, V72 <dbl>, V73 <dbl>, V74 <dbl>, V75 <dbl>, V76 <dbl>, V77 <dbl>, V78 <dbl>, V79 <dbl>,
# V80 <dbl>, V81 <dbl>, V82 <dbl>, V83 <dbl>, V84 <dbl>, V85 <dbl>, V86 <dbl>, V87 <dbl>, V88 <dbl>, V89 <dbl>, V90 <dbl>, V91 <dbl>, V92 <dbl>, V93 <dbl>,
# V94 <dbl>, V95 <dbl>, V96 <dbl>, V97 <dbl>, V98 <dbl>, V99 <dbl>, V100 <dbl>, V101 <dbl>, V102 <dbl>, V103 <dbl>, V104 <dbl>, V105 <dbl>, V106 <dbl>,
# V107 <dbl>, V108 <dbl>, V109 <dbl>, V110 <dbl>, V111 <dbl>, V112 <dbl>, V113 <dbl>, V114 <dbl>, V115 <dbl>, V116 <dbl>, V117 <dbl>, V118 <dbl>, ...