英文:
Mutating a column in R from a particular row subset
问题
I'm here to assist with the translation. Here's the translation of the content you provided:
我正在尝试基于三个其他列创建一个新的数据框列:一个父列、一个特定指示器列和组合父特定指示器的值。
给定:
  parent specific val
1      a        x  10
2      a        y  11
3      a        z  12
4      b        x  20
5      b        y  21
6      b        z  22
7      c        x  30
8      c        y  31
9      c        z  32
我想要创建一个新列,比如px_val(选择每个父级的x值),以便得到的数据框如下所示:
  parent specific val px_val
1      a        x  10     10
2      a        y  11     10
3      a        z  12     10
4      b        x  20     20
5      b        y  21     20
6      b        z  22     20
7      c        x  30     30
8      c        y  31     30
9      c        z  32     30
测试数据框的代码:
df <- data.frame(
  parent=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'),
  specific=c('x', 'y', 'z', 'x', 'y', 'z', 'x', 'y', 'z'),
  val=c(10, 11, 12, 20, 21, 22, 30, 31, 32)
)
我考虑过也许可以迭代数据框,将给定父级的x值存储在变量中,并将其分配给每个父级。但感觉必须有一个更优雅的解决方案?
英文:
I am trying to create a new column in a dataframe based upon three other columns: a parent column, specific indicator column, and the value of the combined parent-specific indicator.
Given:
  parent specific val
1      a        x  10
2      a        y  11
3      a        z  12
4      b        x  20
5      b        y  21
6      b        z  22
7      c        x  30
8      c        y  31
9      c        z  32
I'm looking to create a new column, say px_val (selecting the x value of each parent), so that the resulting dataframe is:
  parent specific val px_val
1      a        x  10     10
2      a        y  11     10
3      a        z  12     10
4      b        x  20     20
5      b        y  21     20
6      b        z  22     20
7      c        x  30     30
8      c        y  31     30
9      c        z  32     30
Code for test df:
df <- data.frame(
  parent=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'),
  specific=c('x', 'y', 'z', 'x', 'y', 'z', 'x', 'y', 'z'),
  val=c(10, 11, 12, 20, 21, 22, 30, 31, 32)
)
I've thought to maybe iterate over the dataframe, storing the x value of a given parent in a variable and assigning that to each parent. But it feels like there has to be a more elegant solution?
答案1
得分: 0
我们可以这样做:
px_val 将包含 specific 等于 x 的每个唯一父级的值 -> val[specific == 'x']
.by=... 仅为此 mutate 分组,优点是我们之后不需要 ungroup():
library(dplyr) #>= dplyr 1.1.0
df %>%
  mutate(px_val = val[specific == 'x'], .by=parent)
  parent specific val px_val
1      a        x  10     10
2      a        y  11     10
3      a        z  12     10
4      b        x  20     20
5      b        y  21     20
6      b        z  22     20
7      c        x  30     30
8      c        y  31     30
9      c        z  32     30
英文:
We could do it this way:
px_val will contain values where specific equals x for each unique parent -> val[specific == 'x']
.by=... groups only for this mutate, the advantage is that we do not need a ungroup() thereafter:
library(dplyr) #>= dplyr 1.1.0
df %>%
  mutate(px_val = val[specific == 'x'], .by=parent)
  parent specific val px_val
1      a        x  10     10
2      a        y  11     10
3      a        z  12     10
4      b        x  20     20
5      b        y  21     20
6      b        z  22     20
7      c        x  30     30
8      c        y  31     30
9      c        z  32     30
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论