ivot_longer 成几对列
我需要跨多组列进行pivot_longer
,创建多个名称-值对。
例如,我需要从这样的东西开始:
df_raw <- tribble(
~id, ~belief_dog, ~belief_bull_frog, ~belief_fish, ~age, ~norm_bull_frog, ~norm_fish, ~norm_dog, ~gender,
"b2x8", 1, 4, 3, 41, 4, 2, 10, 2,
"m89w", 3, 6, 2, 19, 1, 2, 3, 1,
"32x8", 1, 5, 2, 38, 9, 1, 8, 3
)
并将其变成这样的东西:
df_final <- tribble(
~id, ~belief_animal, ~belief_rating, ~norm_animal, ~norm_rating, ~age, ~gender,
"b2x8", "dog", 1, "bull_frog", 4, 41, 2,
"b2x8", "bull_frog", 4, "fish", 2, 41, 2,
"b2x8", "fish", 3, "dog", 10, 41, 2,
"m89w", "dog", 3, "bull_frog", 1, 19, 1,
"m89w", "bull_frog", 6, "fish", 2, 19, 1,
"m89w", "fish", 2, "dog", 3, 19, 1,
"32x8", "dog", 1, "bull_frog", 9, 38, 3,
"32x8", "bull_frog", 5, "fish", 1, 38, 3,
"32x8", "fish", 2, "dog", 8, 38, 3
)
换句话说,任何以“belief_”开头的东西都应该以一个名称为中心——值对和值对。任何以“norm_”开头的内容都应该转换为另一个名称-值对。
我尝试查看其他几个具有相关内容的 Stack Overflow 页面,但无法将这些解决方案转化为这种情况。
任何帮助将不胜感激,强烈偏好dplyr
解决方案。
谢谢!
I'm needing to pivot_longer
across multiple groups of columns, creating multiple names--values pairs.
For instance, I need to go from something like this:
df_raw <- tribble(
~id, ~belief_dog, ~belief_bull_frog, ~belief_fish, ~age, ~norm_bull_frog, ~norm_fish, ~norm_dog, ~gender,
"b2x8", 1, 4, 3, 41, 4, 2, 10, 2,
"m89w", 3, 6, 2, 19, 1, 2, 3, 1,
"32x8", 1, 5, 2, 38, 9, 1, 8, 3
)
And turn it into something lie this:
df_final <- tribble(
~id, ~belief_animal, ~belief_rating, ~norm_animal, ~norm_rating, ~age, ~gender,
"b2x8", "dog", 1, "bull_frog", 4, 41, 2,
"b2x8", "bull_frog", 4, "fish", 2, 41, 2,
"b2x8", "fish", 3, "dog", 10, 41, 2,
"m89w", "dog", 3, "bull_frog", 1, 19, 1,
"m89w", "bull_frog", 6, "fish", 2, 19, 1,
"m89w", "fish", 2, "dog", 3, 19, 1,
"32x8", "dog", 1, "bull_frog", 9, 38, 3,
"32x8", "bull_frog", 5, "fish", 1, 38, 3,
"32x8", "fish", 2, "dog", 8, 38, 3
)
In other words, anything starting with "belief_" should get pivoted in one names--values pair & anything starting with "norm_" should be pivoted into another names--values pair.
I tried looking at several other Stack Overflow pages with somewhat related content but wasn't able to translate those solutions to this situation.
Any help would be appreciated, with a strong preference for dplyr
solutions.
THANKS!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
tidyverse
,您可以以belief
和norm
开头的两组列为中心。然后,使用正则表达式根据第一个下划线进行分组(因为某些列名称有多个下划线)。本质上,我们将信念或规范(列名称中的第一组)放入它们自己的列中(即.value
),然后该组的第二部分(即动物名称)被放入名为animal
的列中。输出
With
tidyverse
, you can pivot on the two sets of columns that starts withbelief
andnorm
. Then, use regex to split into groups according to the first underscore (since some column names have multiple underscores). Essentially, we are puttingbelief
ornorm
(the first group in the column name) into their own columns (i.e.,.value
), then the second part of the group (i.e., animal names) are put into one column namedanimal
.Output
通过更多的实验解决了这个问题!
关键在于
names_to
和names_to
。names_pattern
参数。我不太明白
".value"
或正则表达式"([az]+)_*(.+)"
是如何工作的,但解决方案仍然有效。Solved it with a bit more experimentation!
The key comes down to both the
names_to
& thenames_pattern
arguments.I don't really understand how
".value"
or the regex"([a-z]+)_*(.+)"
work, but the solution works nonetheless.对于这些数据:
For these data: