循环根据 R 中的其他情况创建一个新变量(非常基本)

发布于 2024-11-06 05:09:10 字数 387 浏览 3 评论 0原文

我有一个包含三个变量的数据框:IDgroupnomminated_ID。 我想知道 nomated_ID 所属的 group

我想象对于每种情况,我们采用 nomminated_ID,找到它所在的情况等于ID,然后将原始案例中的nomminated_Group变量设置为等于匹配案例中的group变量。 (如果没有匹配,请将其设置为 NA)

如果这可以在没有循环的情况下完成,我不会感到惊讶,所以我对解决方案持开放态度。非常感谢您的帮助。要知道,我在发帖之前确实尝试过寻找类似的问题。

I have a dataframe with three variables: ID, group, and nominated_ID.
I want to know the group that nominated_ID belongs in.

I'm imagining that for each case, we take nominated_ID, find the case where it is equal to ID, and then set the nominated_Group variable in the original case equal to the group variable in the matched case. (If there is no match, set it to NA)

I wouldn't be surprised if this can be done without a loop, so I'm open-minded about the solution. Thanks so much for your help. Know that I did try to look for similar questions before posting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何以笙箫默 2024-11-13 05:09:10

您可以通过直接将结果分配到 data.frame 中的列来一步实现此目的,而无需使用 cbind

df$nominated_group <- with(df, group[match(nominated_ID, ID)])
df
  ID group nominated_ID nominated_group
1  9   Odd            9             Odd
2  5   Odd            8            <NA>
3  2  Even            4            Even
4  4  Even            9             Odd
5  3   Odd            2            Even

我使用 with 作为引用df 的列,而无需重复写入 df$

You can achieve this in one step without the use of cbind by directly allocating results to a column in your data.frame:

df$nominated_group <- with(df, group[match(nominated_ID, ID)])
df
  ID group nominated_ID nominated_group
1  9   Odd            9             Odd
2  5   Odd            8            <NA>
3  2  Even            4            Even
4  4  Even            9             Odd
5  3   Odd            2            Even

I used with as a convenient way of referring to the columns of df without having to repeatedly write df$.

风为裳 2024-11-13 05:09:10

以下似乎有效;可能有更好的方法

> df <- data.frame(ID = c(9, 5, 2, 4, 3), 
+                  group = c("Odd", "Odd", "Even", "Even", "Odd"),
+                  nominated_ID = c(9, 8, 4, 9, 2)                 )
> df
  ID group nominated_ID
1  9   Odd            9
2  5   Odd            8
3  2  Even            4
4  4  Even            9
5  3   Odd            2
> nominated_Group <- df[match(df$nominated_ID, df$ID), ]$group
> newDF <- cbind(df, nominated_Group)
> newDF
  ID group nominated_ID nominated_Group
1  9   Odd            9             Odd
2  5   Odd            8            <NA>
3  2  Even            4            Even
4  4  Even            9             Odd
5  3   Odd            2            Even

The following seems to work; there may be better ways

> df <- data.frame(ID = c(9, 5, 2, 4, 3), 
+                  group = c("Odd", "Odd", "Even", "Even", "Odd"),
+                  nominated_ID = c(9, 8, 4, 9, 2)                 )
> df
  ID group nominated_ID
1  9   Odd            9
2  5   Odd            8
3  2  Even            4
4  4  Even            9
5  3   Odd            2
> nominated_Group <- df[match(df$nominated_ID, df$ID), ]$group
> newDF <- cbind(df, nominated_Group)
> newDF
  ID group nominated_ID nominated_Group
1  9   Odd            9             Odd
2  5   Odd            8            <NA>
3  2  Even            4            Even
4  4  Even            9             Odd
5  3   Odd            2            Even
少女的英雄梦 2024-11-13 05:09:10

您可以使用 transformmatch 和数组索引以语法紧凑的方式完成此操作。使用@Henry的数据框:

df <- transform( df, nominated_group = group[match(nominated_ID, ID)])

> df
  ID group nominated_ID nominated_group
1  9   Odd            9             Odd
2  5   Odd            8            <NA>
3  2  Even            4            Even
4  4  Even            9             Odd
5  3   Odd            2            Even

You can do this in a syntactically compact way using transform, match and array indexing. Using @Henry's data-frame:

df <- transform( df, nominated_group = group[match(nominated_ID, ID)])

> df
  ID group nominated_ID nominated_group
1  9   Odd            9             Odd
2  5   Odd            8            <NA>
3  2  Even            4            Even
4  4  Even            9             Odd
5  3   Odd            2            Even
极度宠爱 2024-11-13 05:09:10

可能不是最“直观”的方式,但是如果您使用nominal_ID作为第一个副本的合并索引并使用ID作为第一个副本的by索引,则将dfdf合并也可以工作。第二个并保留所有行,您需要删除第二个 nomated_ID 列并重新排列顺序以使内容与上面的答案匹配:

merge(df,df, by.x=3, by.y=1, all.x=TRUE)[order(df$nominated_ID), c(2,3, 1, 4)]

  ID group.x nominated_ID group.y
5  4    Even            9     Odd
3  5     Odd            8    <NA>
2  2    Even            4    Even
1  3     Odd            2    Even
4  9     Odd            9     Odd

Probably not the most "intuitive' way, but merging df against df also works if you use nominated_ID as the merge index for the first copy and ID as the by index for the second and keep all rows. You need to drop the second nominated_ID column and rearrange the order to get things to match the answers above:

merge(df,df, by.x=3, by.y=1, all.x=TRUE)[order(df$nominated_ID), c(2,3, 1, 4)]

  ID group.x nominated_ID group.y
5  4    Even            9     Odd
3  5     Odd            8    <NA>
2  2    Even            4    Even
1  3     Odd            2    Even
4  9     Odd            9     Odd
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文