Dplyr transmitting one value for each group in a mutation

I am trying to do something very similar to Scaling with respect to the value in each group (via dplyr) (however this solution seems to have broken R for me). I would like to reproduce one value for each group and add a new column repeating this value. As an example, I have

library(dplyr)

data = expand.grid(
  category = LETTERS[1:2],
  year = 2000:2003)
data$value = runif(nrow(data))

data

  category year     value
1        A 2000 0.6278798
2        B 2000 0.6112281
3        A 2001 0.2170495
4        B 2001 0.6454874
5        A 2002 0.9234604
6        B 2002 0.9311204
7        A 2003 0.5387899
8        B 2003 0.5573527

And I would like a dataframe like

data

  category year     value    value2
1        A 2000 0.6278798 0.6278798
2        B 2000 0.6112281 0.6112281
3        A 2001 0.2170495 0.6278798
4        B 2001 0.6454874 0.6112281
5        A 2002 0.9234604 0.6278798
6        B 2002 0.9311204 0.6112281
7        A 2003 0.5387899 0.6278798
8        B 2003 0.5573527 0.6112281

i.e. the value for each category is the value since 2000. I tried to come up with a general solution that is extensible for given filtering criteria, i.e. something like

data %>% group_by(category) %>% mutate(value = filter(data, year==2002))

however, this does not work due to incorrect length in the assignment.

+4
source share
1 answer

Do it:

data %>% group_by(category) %>%
  mutate(value2 = value[year == 2000])

:

data %>% group_by(category) %>%
  arrange(year) %>%
  mutate(value2 = value[1])

data %>% group_by(category) %>%
  arrange(year) %>%
  mutate(value2 = first(value))

data %>% group_by(category) %>%
  mutate(value2 = nth(value, n = 1, order_by = "year"))

, , .

mutate(value = filter(data, year==2002)) .

  • data , , , .

  • dplyr , filter. value = filter(...), value.

+10

Source: https://habr.com/ru/post/1618486/


All Articles