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 3722032
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:45:34+00:00 2026-05-19T05:45:34+00:00

I want to filter out all values of var3 < 5 while keeping at

  • 0

I want to filter out all values of var3 < 5 while keeping at least one occurrence of each value of var1.

> foo <- data.frame(var1=c(1, 1, 8, 8, 5, 5, 5), var2=c(1,2,3,2,4,6,8), var3=c(7,1,1,1,1,1,6))
> foo
  var1 var2 var3
1    1    1    7
2    1    2    1
3    8    3    1
4    8    2    1
5    5    4    1
6    5    6    1
7    5    8    6

subset(foo, (foo$var3>=5)) would remove row 2 to 6 and I would have lost var1==8.

  • I want to remove the row if there is another value of var1 that fulfills the condition foo$var3 >= 5. See row 5.
  • I want to keep the row, assiging NA to var2 and var3 if all occurrences of a value var1 do not fulfill the condition foo$var3 >= 5.

This is the result I expect:

  var1 var2 var3
1    1    1    7
3    8   NA   NA
7    5    8    6

This is the closest I got:

> foo$var3[ foo$var3 < 5 ] = NA
> foo$var2[ is.na(foo$var3) ] = NA
> foo
  var1 var2 var3
1    1    1    7
2    1   NA   NA
3    8   NA   NA
4    8   NA   NA
5    5   NA   NA
6    5   NA   NA
7    5    8    6

Now I just need to know how to conditionally remove the right rows (2, 3 or 4, 5, 6): Remove the row if var2 & var3 are NA and if the value of var1 has more than 1 occurrence.

But there is surely a much simpler/elegant way to approach this little problem.

edit: changed foo to resemble my use case more

  • 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-19T05:45:36+00:00Added an answer on May 19, 2026 at 5:45 am

    The fastest way is to use merge:

    > merge(foo[foo$var3>5,],unique(foo$var1),by.x=1,by.y=1,all.y=T)
      var1 var2 var3
    1    1    1    7
    2    5    8    6
    3    8   NA   NA
    

    unique(foo$var1) gives the unique values in var1. These ones are mapped against the dataframe where var3 is larger than five. You take the first column of every argument (all.x=1, all.y=1) and you say that all values in y should be represented (all.y=T). See also ?merge.

    If you want to preserve the order, then :

    > merge(foo[foo$var3>5,],unique(foo$var1),by.x=1,by.y=1,
    + all.y=T)[order(unique(foo$var1)),]
      var1 var2 var3
    1    1    1    7
    3    8   NA   NA
    2    5    8    6
    

    merge sorts the variable on which the mapping happens. order gives this sorting, so you can reverse it using that order as indices. See also ?order.

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

Sidebar

Related Questions

I have a atomic vector and want to filter out all the rows that
I store some data in MySQL and want to filter out rows that match
I want to filter repeated elements in my list for instance foo = ['a','b','c','a','b','d','a','d']
I'm having a table with one XML column. I'd like to filter out the
I have an array which I want to filter out certain keys. Let's say
I am spitting out some values with a for loop and want to make
I have a list of key value pairs. For each key, I want to
I want to filter my model object using two filters. So, it can be
I want to filter a java.util.Collection based on a predicate.
I want to filter e-mail headers received on my Postfix mail receiving server. I

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.