如何在数据框架中迭代因子水平并添加来自列表的值的列?

huangapple go评论76阅读模式
英文:

How can I add a column with values from a list, iterating in a data frame over factor levels?

问题

I can help you translate the code-related parts into Chinese. Here it is:

给定数据框架 df

df <- data.frame(1,
                 1:12,
                 groups = factor(rep(LETTERS[1:4], each = 3)))
df

以及列表 list_group

list_group <- list(
  A = c("Jack","John","Joe"),
  B = c("Ed","Edd","Eddy"),
  C = c("Gianni","Franco","Ugo"),
  D = c("Bob","Rob","Frank"))
list_group

我想要得到以下结果:

如何在数据框架中迭代因子水平并添加来自列表的值的列?

因此,添加列 person,其中包含与 df$groups 水平对应的名称。这需要将行多次复制到因子水平,但要与 list_group 中相应的名称相乘(即 df$groups 水平 "A" = "Jack","John","Joe"; df$groups 水平 "B" = "Ed","Edd","Eddy"; 等等)。

请注意,我只提供了代码的翻译部分,没有包含其他内容。

英文:

I am struggling in performing and iterating the following operation in the R environment.

Given a data frame df:

df &lt;- data.frame(1,
                 1:12,
                 groups = factor(rep(LETTERS[1:4], each = 3)))
df

and the list list_group:

list_group &lt;- list(
  A = c(&quot;Jack&quot;,&quot;John&quot;,&quot;Joe&quot;),
  B = c(&quot;Ed&quot;,&quot;Edd&quot;,&quot;Eddy&quot;),
  C = c(&quot;Gianni&quot;,&quot;Franco&quot;,&quot;Ugo&quot;),
  D = c(&quot;Bob&quot;,&quot;Rob&quot;,&quot;Frank&quot;))
list_group

I would like to get the following result:

如何在数据框架中迭代因子水平并添加来自列表的值的列?

Thus, add the column person with the names in correspondence of the df$groups level. This requires to multiple the rows over the factor levels, but specifically with the corresponding names in the list_group (i.e., df$groups level "A" = "Jack","John","Joe"; df$groups level "B" = "Ed","Edd","Eddy"; etc.)

I went through the function of the Tidyverse, in particular dplyr,tidyr and purrr, but I did not find specific functions for my problem.

Also, I tried to dig into the theory behind Iteration in R, mainly following the guide 21 Iteration, but I did not succeed in that.

答案1

得分: 2

你可以使用 stack + merge

merge(df, stack(list_group), by.x = "groups", by.y = "ind")

或者使用 dplyr 的等效方法:

library(dplyr)
full_join(df, stack(list_group), by = c("groups" = "ind"), relationship = "many-to-many")

输出:

#    groups X1 X1.12 values
# 1       A  1     1   Jack
# 2       A  1     1   John
# 3       A  1     1    Joe
# 4       A  1     2   Jack
# 5       A  1     2   John
# 6       A  1     2    Joe
# 7       A  1     3   Jack
# 8       A  1     3   John
# 9       A  1     3    Joe
# 10      B  1     4     Ed
# 11      B  1     4    Edd
# 12      B  1     4   Eddy
# 13      B  1     5     Ed
# 14      B  1     5    Edd
# 15      B  1     5   Eddy
# 16      B  1     6     Ed
# 17      B  1     6    Edd
# 18      B  1     6   Eddy
# 19      C  1     7 Gianni
# 20      C  1     7 Franco
# 21      C  1     7    Ugo
# 22      C  1     8 Gianni
# 23      C  1     8 Franco
# 24      C  1     8    Ugo
# 25      C  1     9 Gianni
# 26      C  1     9 Franco
# 27      C  1     9    Ugo
# 28      D  1    10    Bob
# 29      D  1    10    Rob
# 30      D  1    10  Frank
# 31      D  1    11    Bob
# 32      D  1    11    Rob
# 33      D  1    11  Frank
# 34      D  1    12    Bob
# 35      D  1    12    Rob
# 36      D  1    12  Frank
英文:

You can use stack + merge:

merge(df, stack(list_group), by.x = &quot;groups&quot;, by.y = &quot;ind&quot;)

Or the dplyr equivalent:

library(dplyr)
full_join(df, stack(list_group), by = c(&quot;groups&quot; = &quot;ind&quot;), relationship = &quot;many-to-many&quot;)

output

#    groups X1 X1.12 values
# 1       A  1     1   Jack
# 2       A  1     1   John
# 3       A  1     1    Joe
# 4       A  1     2   Jack
# 5       A  1     2   John
# 6       A  1     2    Joe
# 7       A  1     3   Jack
# 8       A  1     3   John
# 9       A  1     3    Joe
# 10      B  1     4     Ed
# 11      B  1     4    Edd
# 12      B  1     4   Eddy
# 13      B  1     5     Ed
# 14      B  1     5    Edd
# 15      B  1     5   Eddy
# 16      B  1     6     Ed
# 17      B  1     6    Edd
# 18      B  1     6   Eddy
# 19      C  1     7 Gianni
# 20      C  1     7 Franco
# 21      C  1     7    Ugo
# 22      C  1     8 Gianni
# 23      C  1     8 Franco
# 24      C  1     8    Ugo
# 25      C  1     9 Gianni
# 26      C  1     9 Franco
# 27      C  1     9    Ugo
# 28      D  1    10    Bob
# 29      D  1    10    Rob
# 30      D  1    10  Frank
# 31      D  1    11    Bob
# 32      D  1    11    Rob
# 33      D  1    11  Frank
# 34      D  1    12    Bob
# 35      D  1    12    Rob
# 36      D  1    12  Frank

答案2

得分: 0

你可以使用基础的stack()函数,然后使用整洁的full_join(multiple = "all")函数:

df %>% full_join(
  y = list_group %>% stack(),
  by = join_by(groups == ind),
  multiple = "all"
)

结果:

   X1 X1.12 groups values
1   1     1      A   Jack
2   1     1      A   John
3   1     1      A    Joe
4   1     2      A   Jack
5   1     2      A   John
6   1     2      A    Joe
7   1     3      A   Jack
8   1     3      A   John
9   1     3      A    Joe
10  1     4      B     Ed
11  1     4      B    Edd
12  1     4      B   Eddy
13  1     5      B     Ed
14  1     5      B    Edd
15  1     5      B   Eddy
16  1     6      B     Ed
17  1     6      B    Edd
18  1     6      B   Eddy
19  1     7      C Gianni
20  1     7      C Franco
21  1     7      C    Ugo
22  1     8      C Gianni
23  1     8      C Franco
24  1     8      C    Ugo
25  1     9      C Gianni
26  1     9      C Franco
27  1     9      C    Ugo
28  1    10      D    Bob
29  1    10      D    Rob
30  1    10      D  Frank
31  1    11      D    Bob
32  1    11      D    Rob
33  1    11      D  Frank
34  1    12      D    Bob
35  1    12      D    Rob
36  1    12      D  Frank
英文:

You can use base stack() and then a tidy full_join(multiple = &quot;all&quot;):

df %&gt;% full_join(
  y = list_group %&gt;% stack(),
  by = join_by(groups == ind),
  multiple = &quot;all&quot;
)

Result:

   X1 X1.12 groups values
1   1     1      A   Jack
2   1     1      A   John
3   1     1      A    Joe
4   1     2      A   Jack
5   1     2      A   John
6   1     2      A    Joe
7   1     3      A   Jack
8   1     3      A   John
9   1     3      A    Joe
10  1     4      B     Ed
11  1     4      B    Edd
12  1     4      B   Eddy
13  1     5      B     Ed
14  1     5      B    Edd
15  1     5      B   Eddy
16  1     6      B     Ed
17  1     6      B    Edd
18  1     6      B   Eddy
19  1     7      C Gianni
20  1     7      C Franco
21  1     7      C    Ugo
22  1     8      C Gianni
23  1     8      C Franco
24  1     8      C    Ugo
25  1     9      C Gianni
26  1     9      C Franco
27  1     9      C    Ugo
28  1    10      D    Bob
29  1    10      D    Rob
30  1    10      D  Frank
31  1    11      D    Bob
32  1    11      D    Rob
33  1    11      D  Frank
34  1    12      D    Bob
35  1    12      D    Rob
36  1    12      D  Frank

huangapple
  • 本文由 发表于 2023年6月8日 15:51:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定