I’m working with a list in R and I’ve noticed an unexpected difference in output when using single and double bracket indexing in the case_when() function from the dplyr package. Here’s the sample list I’m working with:
list1 <- list(a = as.data.frame(cbind(1:5,6:10)), b = "Hello", c = list(x = 10, y =20))
When I use double bracket indexing in case_when(), I get one result:
case_when(list1[[1]][['V1']]==1~'a',
list1[[1]][['V1']]==3~NA,
list1[[1]][['V1']] %in% c(2:4)~'b')
But when I use single bracket indexing in case_when(), I get a different result:
case_when(list1[[1]]['V1']==1~'a',
list1[[1]]['V1']==3~NA,
list1[[1]]['V1'] %in% c(2:4)~'b')
Can anyone explain why there’s a difference in output between these two methods of indexing in case_when()? Any insights would be greatly appreciated! I think the difference is related to ‘%in%’.