[英]How to select the first value of a column of a data frame according to the group using dplyr?
I have 2 frames of data which I joined using the left_join()
function.我有 2 帧数据,我使用
left_join()
函数加入了left_join()
数据。 Then, I grouped the data by Group using the group_by()
function.然后,我使用
group_by()
函数按组对数据进行分组。 Using the mutate()
function I want to create a column to repeatedly display the first value of column V2 according to the sort group.使用
mutate()
函数我想创建一个列,根据排序组重复显示列 V2 的第一个值。
In MWE the first value of V2
for Group 1 is 5 and for Group 2 it is 7.5.在 MWE 中,第 1 组
V2
的第一个值是 5,而第 2 组是 7.5。 However, the code I wrote for this is selecting the first value from column V2 and repeating for both groups without separating as I want.但是,我为此编写的代码是从 V2 列中选择第一个值并为两个组重复,而不按我的需要分开。
Note: it is simple because it seems to copy column V2
but this selection of the first value is necessary for me to do other calculations.注意:这很简单,因为它似乎复制了
V2
列,但是选择第一个值对我进行其他计算是必要的。
Any tips?有小费吗?
library(dplyr)
Group <- c(1, 2)
V1 <- c(10, 20, 30)
V2 <- c(5, 7.5)
df1 <- expand.grid(V1 = V1,
Group = Group)
df2 <- data.frame(Group, V2)
df <- df1 %>%
left_join(df2) %>%
group_by(Group) %>%
mutate(first = first(.$V2))
V1 ![]() |
Group![]() |
V2 ![]() |
first![]() |
The first column I want![]() first 列 |
---|---|---|---|---|
10 ![]() |
1 ![]() |
5.0 ![]() |
5 ![]() |
5.0 ![]() |
20 ![]() |
1 ![]() |
5.0 ![]() |
5 ![]() |
5.0 ![]() |
30 ![]() |
1 ![]() |
5.0 ![]() |
5 ![]() |
5.0 ![]() |
10 ![]() |
2 ![]() |
7.5 ![]() |
5 ![]() |
7.5 ![]() |
20 ![]() |
2 ![]() |
7.5 ![]() |
5 ![]() |
7.5 ![]() |
30 ![]() |
2 ![]() |
7.5 ![]() |
5 ![]() |
7.5 ![]() |
Remove the .$
and it will work as .$
get the entire column breaking the group attribute and thus the first
will be the first row value of the entire column删除
.$
,它将作为.$
获取整列打破组属性,因此第first
将是整列的第一行值
library(dplyr)
df1 %>%
left_join(df2) %>%
group_by(Group) %>%
mutate(first = first(V2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.