问题描述
我的数据框 DF
看起来像:
ID Name1 Name2 Group
1234 A1 x
1234 A4 w
1234 A3 q
1234 A A
1234 A2 z
5678 B3 s
5678 B B
...
我需要为组添加一列
对于与 ID $ c匹配的每个
ID
的 Name1
$ c>在 Name1 == Name2
的行中。
I need to add a column for Group
that is Name1
for each ID
that matches the ID
in the row where Name1 == Name2
.
所以逻辑是检查Name1 = = Name2,记住该行的ID和Name1值,然后对于具有该ID的每一行,将Name1值放在组列的每一行。
So the logic would be to check if Name1 == Name2, remember the ID of that Row and the Name1 value, then for every row having that ID, put the Name1 Value in each row of the Group column.
结果应该如下所示:
ID Name1 Name2 Group
1234 A1 x A
1234 A4 w A
1234 A3 q A
1234 A A A
1234 A2 z A
5678 B3 s B
5678 B B B
...
我不知道如何在数据框中做到这一点,从许多具有不同ID的行。我不想使用循环。
I am not sure how to do this in the dataframe though and from Many rows with different IDs. I dont want to use loops.
mutate()
或 lapply() code>可能?
mutate()
or lapply()
maybe?
我可以看到如何在Name列中为Name1添加Name1值,名称1 = Name2,但是如何滚动备份所有匹配的ID?
I can see how to add the Name1 value in the Group column for the rows where Name1==Name2, but how do I roll that back up for all matching IDs?
推荐答案
可以使用数据在一行中进行。表
DT[, Group := Name1[Name1 == Name2], by=ID]
全部详细信息:
Full details:
library(data.table)
DT <- as.data.table(DF)
DT[, Group := Name1[Name1 == Name2], by=ID]
ID Name1 Name2 Group
1: 1234 A1 x A
2: 1234 A4 w A
3: 1234 A3 q A
4: 1234 A A A
5: 1234 A2 z A
6: 5678 B3 s B
7: 5678 B B B
8: 1589 C x NA
9: 1589 C y NA
## if `Name1`, `Name2` are NOT characters, use
DT[, Name1 := as.character(Name1)]
DT[, Name2 := as.character(Name2)]
这篇关于根据其他列的值将列添加到数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!