Closed kimpro82 closed 8 months ago
any()
Function Practice Examples:Checking if any element in a vector is greater than 5:
x <- c(1, 2, 3, 4, 6, 7)
any(x > 5) # TRUE
Checking if any element in a vector is even:
y <- c(1, 3, 5, 7, 9)
any(y %% 2 == 0) # FALSE
Checking if any element in a list is a string:
z <- list(1, "hello", TRUE, c(1, 2, 3))
any(sapply(z, is.character)) # TRUE
Checking if any element in a data frame column is missing:
df <- data.frame(name = c("John", "Jane", NA), age = c(30, 25, NA))
any(is.na(df$age)) # TRUE
Checking if any student in a class scored above 90 on a test:
scores <- c(85, 92, 78, 95, 82)
any(scores > 90) # TRUE
Checking if any employee in a company has a salary greater than $100,000:
salaries <- c(80000, 120000, 95000, 75000)
any(salaries > 100000) # TRUE
Checking if any fruit in a basket is an apple:
fruits <- c("banana", "orange", "apple", "mango")
any(fruits == "apple") # TRUE
Checking if any city in a list has a population greater than 1 million:
cities <- list(
city = c("New York", "London", "Tokyo"),
population = c(8804190, 8638686, 37435191)
)
any(cities$population > 1000000) # TRUE
These examples demonstrate the versatility of the any() function in testing various conditions across different data structures in R. Feel free to modify and expand upon these examples to further your understanding and practice of the any() function.
References