简体   繁体   English

如何使用dplyr根据组选择数据框列的第一个值?

[英]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 V1 Group团体 V2 V2 first第一的 The first column I want我想要的first
10 10 1 1 5.0 5.0 5 5 5.0 5.0
20 20 1 1 5.0 5.0 5 5 5.0 5.0
30 30 1 1 5.0 5.0 5 5 5.0 5.0
10 10 2 2 7.5 7.5 5 5 7.5 7.5
20 20 2 2 7.5 7.5 5 5 7.5 7.5
30 30 2 2 7.5 7.5 5 5 7.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.

相关问题 R:根据 dplyr 的列值打破 data.frame - R: Break a data.frame according to value of column with dplyr 如何根据 R 中的另一个数据框匹配列的值并使用 dplyr 打印消息? - How can I match the values of a column according to another data frame in R and print a message using dplyr? 如何使用 dplyr 根据 R 中的另一个数据框匹配列的值? - How can i match the values of a column according to another of a data frame in R using dplyr? 如何使用dplyr通过id过滤数据框组中列的前10个百分位数 - how to filter top 10 percentile of a column in a data frame group by id using dplyr 使用R根据数据帧中的列的值的频率对数据进行分组 - Group data according to frequency of values in a column in a data frame using R 如何使用 dplyr 访问/选择嵌套数据框列 - How to access/select nested data frame column with dplyr 使用dplyr中的select按行值对数据帧中的子集列 - subset columns in a data frame by a row value using select in dplyr 如何使用 dplyr 管道添加前导零以选择数据框列中的行 - How to add leading zeros to select rows in a data frame column using dplyr pipes 使用 dplyr 在过滤数据框的列中查找最大值 - Finding the largest value in a column of a filtered data frame using dplyr R:使用dplyr基于列值的子集data.frame - R: subset data.frame based on column value using dplyr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM