1

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?

2
  • The j is not an index for you to do j + 1
    – akrun
    Jun 19, 2018 at 14:36
  • Also, apply requires some dimensional data instead of a vector
    – akrun
    Jun 19, 2018 at 14:45

1 Answer 1

1

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
1
  • 1
    Thanks! lead and lag were the key functions I needed to be informed about!
    – pd441
    Jun 19, 2018 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.