根据列中的条件将值分配给组 [英] Assign value to group based on condition in column

查看:60
本文介绍了根据列中的条件将值分配给组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框架,如下所示:

I have a data frame that looks like the following:

> df = data.frame(group = c(1,1,1,2,2,2,3,3,3), 
                 date = c(1,2,3,4,5,6,7,8,9),
                 value = c(3,4,3,4,5,6,6,4,9))
> df
  group date value
1     1    1     3
2     1    2     4
3     1    3     3
4     2    4     4
5     2    5     5
6     2    6     6
7     3    7     6
8     3    8     4
9     3    9     9

我想创建一个新列,其中包含与值列中的值 4关联的每个组的日期值。

I want to create a new column that contains the date value per group that is associated with the value "4" from the value column.

以下数据框显示了我希望实现的目标。

The following data frame shows what I hope to accomplish.

  group date value newValue
1     1    1     3        2
2     1    2     4        2
3     1    3     3        2
4     2    4     4        4
5     2    5     5        4
6     2    6     6        4
7     3    7     6        8
8     3    8     4        8
9     3    9     9        8

如我们所见,组1具有 newValue 2,因为那是与值 4。同样,第二组具有 newValue 4,第三组具有 newValue 8。

As we can see, group 1 has the newValue "2" because that is the date associated with the value "4". Similarly, group two has newValue 4 and group three has newValue 8.

我认为有一种简单的方法可以使用 ave()或范围为 dplyr / data.table 函数,但是我的多次尝试都没有成功。

I assume there is an easy way to do this using ave() or a range of dplyr/data.table functions, but I have been unsuccessful with my many attempts.

推荐答案

这是一个快速的 data.table 一个

library(data.table)
setDT(df)[, newValue := date[value == 4L], by = group]
df
#    group date value newValue
# 1:     1    1     3        2
# 2:     1    2     4        2
# 3:     1    3     3        2
# 4:     2    4     4        4
# 5:     2    5     5        4
# 6:     2    6     6        4
# 7:     3    7     6        8
# 8:     3    8     4        8
# 9:     3    9     9        8






以下是类似的 dplyr 版本

library(dplyr)
df %>%
  group_by(group) %>%
  mutate(newValue = date[value == 4L])






或过滤数据后使用 merge 的可能的基本R解决方案(


Or a possible base R solution using merge after filtering the data (will need some renaming afterwards)

merge(df, df[df$value == 4, c("group", "date")], by = "group")

这篇关于根据列中的条件将值分配给组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆