0

Here is an example illustrating the desired Out, which currently gives all T

Eg <- tibble("A"=  c("A","B", "C"),
            "Number" = 1:3,
             "Lists" = c(tibble(A=c(1,5)),tibble(A=1),tibble(B=2)))
Out <- Eg%>%
  mutate(New= case_when(length(Lists)==2~F,
                        T~T))

#My desired Out would be
print(Out$New)
F T T
JCran
  • 375
  • 2
  • 14

1 Answers1

2

You can use lengths :

Eg$New <- lengths(Eg$Lists) != 2
Eg

# A tibble: 3 x 4
#  A     Number Lists        New  
#  <chr>  <int> <named list> <lgl>
#1 A          1 <dbl [2]>    FALSE
#2 B          2 <dbl [1]>    TRUE 
#3 C          3 <dbl [1]>    TRUE 

If you have to use dplyr::case_when :

library(dplyr)
Eg %>%
  rowwise() %>%
  mutate(New= case_when(length(Lists)==2~FALSE,
                        TRUE~TRUE))

Or

Eg %>%
  mutate(New= case_when(lengths(Lists)==2~FALSE,
                        TRUE~TRUE))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213