Daniel Valencia C.
Daniel Valencia C.

Reputation: 2279

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. Then, I grouped the data by Group using the group_by() function. Using the mutate() function I want to create a column to repeatedly display the first value of column V2 according to the sort group.

In MWE the first value of V2 for Group 1 is 5 and for Group 2 it is 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.

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.

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
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

Upvotes: 3

Views: 2396

Answers (1)

akrun
akrun

Reputation: 887048

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

library(dplyr)
df1 %>%
  left_join(df2) %>%
  group_by(Group) %>%
  mutate(first = first(V2))

Upvotes: 3

Related Questions