any and all function in R

  • any() function in R checks whether at least one value remains true of the logical vector
  • all() function in R checks whether all values remains true of the logical vector

 

Example of any function in R: 

# any function in R

v1 <- c(3,4)
v2 <- c(101,4)
t <- c(1,2,3,4,5,6,7,8)
any(v1 %in% t)

the above example checks whether any value of v1 present in t so the output will be

[1] TRUE

 

 

# any function in R

any(v2 %in% t)

the above example checks whether any value of v2 present in t so the output will be

[1] TRUE

 

Example of all function in R:

# all function in R

all(v2 %in% t)

the above example checks whether all value of v2 present in t so the output will be

[1] FALSE

 

Example of any function in R for data frame:

Lets use same mtcars data frame to depict example of any function in R

# any function in R for mtcars data frame

any(mtcars$cyl %in% 4)

When we execute above code it checks whether atleast one value of cyl column of mtcars data frame have value 4 in it

So the output will be

[1] TRUE

 

 

Example of all function in R for data frame:

Lets use same mtcars data frame to depict example of all function in R

# all function in R for mtcars data frame

all(mtcars$cyl %in% 4)

When we execute above code it checks whether all the values of cyl column of mtcars data frame have value 4 in it

So the output will be

[1] FALSE

 

 

# all function in R for mtcars data frame

all(mtcars$am %in% c(1,0))

When we execute above code it checks whether all the values of am column of mtcars data frame have value 0 or 1 in it, which is TRUE

So the output will be

[1] TRUE

previous small any and all function in r                                                                                                           next small any and all function in r

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts