Home > database >  R: How to replace all rows in a group with a value from first row in a df [duplicate]
R: How to replace all rows in a group with a value from first row in a df [duplicate]

Time:09-30

I have a df, and I would like to replace all the rows in a group with the value from the first instance/row pr group.

So eg. for the mtcars dataset, I would like for each group of carb to create a new variable carb_1, that replaces all values in the column for carb with the value from qsec with the first row for carb (eg. 18.61 for carb==1):

library(dplyr)
mtcars %>%
  arrange(carb, qsec)

I have tried with a dplyr combo of mutate and top_n, but it does not provide the right results:


mtcars %>%
   group_by(carb)%>%
   mutate(carb_1 = top_n(mtcars, 1,qsec ))

#or

mtcars %>%
   group_by(carb)%>%
   mutate(carb_1 = top_n(mtcars, 1,carb))

The expected results are something like this for carb==1, for all rows with carb==2: 16.70 and so on...

df_expected <- mtcars
df_expected$carb_1 <- ifelse(df_expected$carb==1, 18.61, df_expected$carb)
df_expected

Thanks a lot in advance!

CodePudding user response:

There's a first function in dplyr

mtcars %>%
  group_by(carb)%>%
  mutate(carb_1 = first(qsec))

Though if you are ordering by qsec you could also just use min

  • Related