Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7689191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:06:25+00:00 2026-05-31T20:06:25+00:00

I have a data frame with an id column and some (potentially many) columns

  • 0

I have a data frame with an id column and some (potentially many) columns with values, here ‘v1’, ‘v2’:

df <- data.frame(id = c(1:5), v1 = c(0,15,9,12,7), v2 = c(9,32,6,17,11))
#   id v1 v2
# 1  1  0  9
# 2  2 15 32
# 3  3  9  6
# 4  4 12 17
# 5  5  7 11
  1. How can I extract rows where ALL values are larger than a certain value, say 10, which should return:

    #   id v1 v2
    # 2  2 15 32
    # 4  4 12 17
    
  2. How can I extract rows with ANY (at least one) value is larger than 10:

    #   id v1 v2
    # 2  2 15 32
    # 4  4 12 17
    # 5  5  7 11
    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:06:26+00:00Added an answer on May 31, 2026 at 8:06 pm

    See functions all() and any() for the first and second parts of your questions respectively. The apply() function can be used to run functions over rows or columns. (MARGIN = 1 is rows, MARGIN = 2 is columns, etc). Note I use apply() on df[, -1] to ignore the id variable when doing the comparisons.

    Part 1:

    > df <- data.frame(id=c(1:5), v1=c(0,15,9,12,7), v2=c(9,32,6,17,11))
    > df[apply(df[, -1], MARGIN = 1, function(x) all(x > 10)), ]
      id v1 v2
    2  2 15 32
    4  4 12 17
    

    Part 2:

    > df[apply(df[, -1], MARGIN = 1, function(x) any(x > 10)), ]
      id v1 v2
    2  2 15 32
    4  4 12 17
    5  5  7 11
    

    To see what is going on, x > 10 returns a logical vector for each row (via apply() indicating whether each element is greater than 10. all() returns TRUE if all element of the input vector are TRUE and FALSE otherwise. any() returns TRUE if any of the elements in the input is TRUE and FALSE if all are FALSE.

    I then use the logical vector resulting from the apply() call

    > apply(df[, -1], MARGIN = 1, function(x) all(x > 10))
    [1] FALSE  TRUE FALSE  TRUE FALSE
    > apply(df[, -1], MARGIN = 1, function(x) any(x > 10))
    [1] FALSE  TRUE FALSE  TRUE  TRUE
    

    to subset df (as shown above).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame where some of the columns contain NA values. How
I have a data frame with two columns. First column contains categories such as
I have a data frame with 50000 rows and 200 columns. There are duplicate
I have a data frame with several columns; some numeric and some character. How
I have a data frame with some very long comments columns. When I have
I have a data frame where one particular column has a set of specific
I have a date column in a data frame in chr format as follows:
I have a data.frame with 2 columns: Node A, Node B. Each entry in
I have two data frames with two columns each. The first column is timestamps
Let's assume that we have a data frame x which contains the columns job

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.