Home > Net >  Create new variable based on condition per group id
Create new variable based on condition per group id

Time:10-02

I want to create a new column called newcount by taking the value of count at the first observed value for type in each group id.

df<-data.frame(id = c(1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,3,3),
type = c("x","x","y","x","x","x","x","y","y","x","x","x","x","y","x","y","x","x"),
count = c(4,8,7,10,9,5,4,6,5,9,4,2,3,5,8,7,4,9))

Desired output:

 id type  count  newcount
  1    x     4        7
  1    x     8        7
  1    y     7        7
  1    x    10        7
  2    x     9        6
  2    x     5        6
  2    y     4        6
  2    x     6        6
  2    y     5        6
  3    x     9        5
  3    x     4        5
  3    x     2        5
  3    x     3        5
  3    y     5        5
  3    x     8        5
  3    y     7        5
  3    x     4        5
  3    x     9        5

CodePudding user response:

Have you tried something like:

df %>% 
  group_by(id) %>%
  mutate(newcount = count[which(type == "y")[1]]) %>%
  ungroup()
      id type  count newcount
   <dbl> <chr> <dbl>    <dbl>
 1     1 x         4        7
 2     1 x         8        7
 3     1 y         7        7
 4     1 x        10        7
 5     2 x         9        6
 6     2 x         5        6
 7     2 x         4        6
 8     2 y         6        6
 9     2 y         5        6
10     3 x         9        5
11     3 x         4        5
12     3 x         2        5
13     3 x         3        5
14     3 y         5        5
15     3 x         8        5
16     3 y         7        5
17     3 x         4        5
18     3 x         9        5

(I do not obtain the same results as you propose but I think the initial data is not exactly the same)

Hope it is what you wanted!

CodePudding user response:

Do you mean get the count values at first observed y value in each group id ?

Here is a way using match.

library(dplyr)

df %>%
  group_by(id) %>%
  mutate(newcount = count[match('y', type)]) %>%
  ungroup

#     id type  count newcount
#   <dbl> <chr> <dbl>    <dbl>
# 1     1 x         4        7
# 2     1 x         8        7
# 3     1 y         7        7
# 4     1 x        10        7
# 5     2 x         9        6
# 6     2 x         5        6
# 7     2 x         4        6
# 8     2 y         6        6
# 9     2 y         5        6
#10     3 x         9        5
#11     3 x         4        5
#12     3 x         2        5
#13     3 x         3        5
#14     3 y         5        5
#15     3 x         8        5
#16     3 y         7        5
#17     3 x         4        5
#18     3 x         9        5
  • Related