将多列变成单行:逐行旋转更宽
我一直在尝试获取包含 25 个单词的所有组合的一行,但我需要一些帮助才能以正确的顺序排列这些单词。
到目前为止,我有一个如下所示的表格,其中每个单词在 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(...)
I've been trying to get a single row containing every combination of 25 words, but I need some help getting these in the right order.
So far, I have a table looking like this, where each word is repeated 25 times consecutively in Var1
, and Var2
matches Var1
with the other 24 words.
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
...
I need to convert it to one row, with values alternating from Var1 and Var2 (so combining by row) like this:
V1 V2 V3 V4 V5 V6 V25 V26 V27 V28 V29 V30
laugh area laugh game laugh river ... area laugh area game area river ...
I have tried using:
words %>% pivot_wider(names_from = id, values_from = c(Var1, Var2)
However it gives me the words by column rather than by row, i.e.,
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 ...
I tried adding names_vary = "slowest"
to pivot_wider()
, but got the error:
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(...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)