行跨行的有条件突变(按组/ID)?

发布于 2025-02-01 04:47:01 字数 852 浏览 2 评论 0原文

我有一个很大的数据集,我希望提供一些帮助。下面给出了一个示例:

   id  id_row  material
1  1     1     1
2  1     2     1
3  1     3     1
4  2     1     1
5  2     2     2
6  2     3     1
7  3     1     1
8  3     2     1
9  3     3     1
10 4     1     1
11 4     2     2 

我想根据相同ID(跨行)的材料中的材料值添加一个新列。在新的COLUM中,我希望所有具有材料中值1和2的ID(跨行)(例如,值为99),如果不存在,则返回1或2。 类似的事情:

   id  id_row  material  new_column
1  1     1     1             1
2  1     2     1             1
3  1     3     1             1
4  2     1     1             99
5  2     2     2             99
6  2     3     1             99
7  3     1     2             2
8  3     2     2             2
9  3     3     2             2
10 4     1     1             99
11 4     2     2             99

我一直在网上寻找解决方案,没有任何运气,并且尝试使用dplyr和group_by,mutate和ifelse没有任何运气。先感谢您!

I have a large dataset that I would like some help with. An example is given below:

   id  id_row  material
1  1     1     1
2  1     2     1
3  1     3     1
4  2     1     1
5  2     2     2
6  2     3     1
7  3     1     1
8  3     2     1
9  3     3     1
10 4     1     1
11 4     2     2 

I would like to add a new column based on the values in material for the same id (across rows). In the new colum, I would like all id with values 1 and 2 in material (across rows) to be identified (e.g. as value 99) and if not both are present then return either 1 or 2.
Something like this:

   id  id_row  material  new_column
1  1     1     1             1
2  1     2     1             1
3  1     3     1             1
4  2     1     1             99
5  2     2     2             99
6  2     3     1             99
7  3     1     2             2
8  3     2     2             2
9  3     3     2             2
10 4     1     1             99
11 4     2     2             99

I have been looking online for a solution without any luck as well as tried using dplyr and group_by, mutate and ifelse without any luck. Thank you in advance!

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

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

发布评论

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

评论(1

泛滥成性 2025-02-08 04:47:01

尝试以下方法:

library(tidyverse)

tribble(
  ~id, ~id_row, ~material,
  1, 1, 1,
  1, 2, 1,
  1, 3, 1,
  2, 1, 1,
  2, 2, 2,
  2, 3, 1,
  3, 1, 2,
  3, 2, 2,
  3, 3, 2,
  4, 1, 1,
  4, 2, 2
) |> 
  group_by(id) |> 
  mutate(new_column = if_else(any(material == 2) & any(material == 1), 99, NA_real_),
         new_column = if_else(is.na(new_column), material, new_column))
#> # A tibble: 11 × 4
#> # Groups:   id [4]
#>       id id_row material new_column
#>    <dbl>  <dbl>    <dbl>      <dbl>
#>  1     1      1        1          1
#>  2     1      2        1          1
#>  3     1      3        1          1
#>  4     2      1        1         99
#>  5     2      2        2         99
#>  6     2      3        1         99
#>  7     3      1        2          2
#>  8     3      2        2          2
#>  9     3      3        2          2
#> 10     4      1        1         99
#> 11     4      2        2         99

在2022-05-25创建 reprex package (v2.0.1)< /sup>

Try this approach:

library(tidyverse)

tribble(
  ~id, ~id_row, ~material,
  1, 1, 1,
  1, 2, 1,
  1, 3, 1,
  2, 1, 1,
  2, 2, 2,
  2, 3, 1,
  3, 1, 2,
  3, 2, 2,
  3, 3, 2,
  4, 1, 1,
  4, 2, 2
) |> 
  group_by(id) |> 
  mutate(new_column = if_else(any(material == 2) & any(material == 1), 99, NA_real_),
         new_column = if_else(is.na(new_column), material, new_column))
#> # A tibble: 11 × 4
#> # Groups:   id [4]
#>       id id_row material new_column
#>    <dbl>  <dbl>    <dbl>      <dbl>
#>  1     1      1        1          1
#>  2     1      2        1          1
#>  3     1      3        1          1
#>  4     2      1        1         99
#>  5     2      2        2         99
#>  6     2      3        1         99
#>  7     3      1        2          2
#>  8     3      2        2          2
#>  9     3      3        2          2
#> 10     4      1        1         99
#> 11     4      2        2         99

Created on 2022-05-25 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文