pd441
pd441

Reputation: 2763

Nest apply function within tapply

I would like to use tapply to group the outcome of a function according to a variable. The function that I think I need to pass to tapplyis, I believe, apply. I want this function to assign a value depending on the presence of another value in a different variable (var1), however if the value of var1 is something else on the next row (within the grouping) then I would like to assign a different value. Here is what I have attempted, but it doesn't work. I'm not sure if that's because there's something wrong with my approach or because the function I apply is incorrect.

#example data
df.examp <- tibble(id = c(rep(1, 4), rep(2, 4), rep(3, 4)),
   var1 = c('a','a','b','b','a','a','a','a','b','b','b','b'))

#my attempt
df.examp$var2 <- tapply(df.examp$var1, df.examp$id,
                        apply(df.examp$var1, 1, function(j)
                          if (j == 'a'){
                            'foo'
                          } else if (j == 'a' & j + 1 == 'b'){
                            'bar'
                          } else {
                            'other'
                          }
                        )

)

#hoped for outcome
df.examp <- mutate(df.examp, var2 = c(rep('bar', 4), rep('foo', 4), rep('other', 4)))

Does anyone have any ideas where this is going wrong?

Upvotes: 1

Views: 497

Answers (1)

akrun
akrun

Reputation: 887311

We could case_when after grouping by 'id'

library(dplyr)
df.examp %>% 
  group_by(id) %>%
  mutate(var2 = case_when(any(var1 == 'a' & lead(var1) == 'b') ~ 'bar', 
         var1 == 'a' ~ 'foo', 
         TRUE ~ 'other'))
# A tibble: 12 x 3
# Groups:   id [3]
#      id var1  var2 
#   <dbl> <chr> <chr>
# 1     1 a     bar  
# 2     1 a     bar  
# 3     1 b     bar  
# 4     1 b     bar  
# 5     2 a     foo  
# 6     2 a     foo  
# 7     2 a     foo  
# 8     2 a     foo  
# 9     3 b     other
#10     3 b     other
#11     3 b     other
#12     3 b     other

Upvotes: 1

Related Questions