Search code examples
rlistvectorpredicate

R check if a list of TRUE/FALSE values evaluate to TRUE


I am relatively new to R so sorry if this is basics. I have this loop that have a condition inside that compares between 2 lists and return a vector of TRUE/FALSE values. What I really need to check is if this vector includes ONLY TRUE values (meaning the 2 lists are identical)

for ( i in 1:(length(sessions_items_list)-1)){
  if (sessions_items_list[[i]]==sessions_items_list[[i+1]]){
    identical_sessions_df [i,1] <- names(sessions_items_list[i])
    identical_sessions_df [i,2] <- names(sessions_items_list[i+1])
    #identical_sessions_df [i,3] <- sessions_items_list[[i]]
    #identical_sessions_df [i,4] <- sessions_items_list[[i+1]]
  }
}

Here is some data:

> sessions_items_list[[2]]
 [1] "111502665618" "111505397996" "121238758674" "121480200508" "131159477858" "161469302097" "161474935929" "171526802604" "231197187139" "231381216285" "251502101205" "261650031415"
[13] "261652085962" "261652452940" "271538491767" "281398254987" "291227243345" "311065441334" "321561638226" "321566237993" "331042848072" "331251405185" "331366646532" "361096154736"
[25] "381043841996"
> sessions_items_list[[3]]
 [1] "111502665618" "111505397996" "121238758674" "131159477858" "161469302097" "161474935929" "171526802604" "231197187139" "231381216285" "251502101205" "261647474153" "261650031415"
[13] "261652085962" "261652452940" "271538491767" "281398254987" "291227243345" "311065441334" "321561638226" "321566237993" "331042848072" "331251405185" "331366646532" "361096154736"
[25] "381043841996"

Here is the condition result example:

> sessions_items_list[[2]]==sessions_items_list[[3]]
 [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

Is there a simple function to check of this list of predicates evaluate to TRUE (all values are TRUE)?


Solution

  • Maybe try the all function:

    > x <- c(rep(TRUE, 5), FALSE)
    > y <- c(rep(TRUE, 6))
    > all(x)
    [1] FALSE
    > all(y)
    [1] TRUE
    

    As its name says, it checks if all the values are TRUE.