kimpro82 / MyPractice

Born in October and learn like octopus
0 stars 0 forks source link

R : `any()` & `all()` Practice #304

Closed kimpro82 closed 8 months ago

kimpro82 commented 8 months ago

References

kimpro82 commented 8 months ago

By Gemini

R any() Function Practice Examples:

  1. Checking if any element in a vector is greater than 5:

    x <- c(1, 2, 3, 4, 6, 7)
    any(x > 5) # TRUE
  2. Checking if any element in a vector is even:

    y <- c(1, 3, 5, 7, 9)
    any(y %% 2 == 0) # FALSE
  3. 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
  4. 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
  5. Checking if any student in a class scored above 90 on a test:

    scores <- c(85, 92, 78, 95, 82)
    any(scores > 90) # TRUE
  6. Checking if any employee in a company has a salary greater than $100,000:

    salaries <- c(80000, 120000, 95000, 75000)  
    any(salaries > 100000) # TRUE
  7. Checking if any fruit in a basket is an apple:

    fruits <- c("banana", "orange", "apple", "mango")
    any(fruits == "apple") # TRUE
  8. 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.