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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:05:18+00:00 2026-05-28T07:05:18+00:00

I am a new guy in R and really unsure how to filter data

  • 0

I am a new guy in R and really unsure how to filter data in date frame.

I have created a data frame with two columns including monthly date and corresponding temperature. It has a length of 324.

> head(Nino3.4_1974_2000)
  Month_common               Nino3.4_degree_1974_2000_plain
1   1974-01-15                       -1.93025
2   1974-02-15                       -1.73535
3   1974-03-15                       -1.20040
4   1974-04-15                       -1.00390
5   1974-05-15                       -0.62550
6   1974-06-15                       -0.36915

The filter rule is to select the temperature which are greater or equal to 0.5 degree. Also, it has to be at least continuously 5 months.

I have eliminate the data with less than 0.5 degree temperature (see below).

for (i in 1) {
el_nino=Nino3.4_1974_2000[which(Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain >= 0.5),]
}

> head(el_nino)
   Month_common               Nino3.4_degree_1974_2000_plain
32   1976-08-15                      0.5192000
33   1976-09-15                      0.8740000
34   1976-10-15                      0.8864501
35   1976-11-15                      0.8229501
36   1976-12-15                      0.7336500
37   1977-01-15                      0.9276500

However, i still need to extract continuously 5 months. I wish someone could help me out.

  • 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-28T07:05:18+00:00Added an answer on May 28, 2026 at 7:05 am

    If you can always rely on the spacing being one month, then let’s temporarily discard the time information:

    temps <- Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain
    

    So, since every temperature in that vector is always separated by one month, we just have to look for runs where the temps[i]>=0.5, and the run has to be at least 5 long.

    If we do the following:

    ofinterest <- temps >= 0.5
    

    we’ll have a vector ofinterest with values TRUE FALSE FALSE TRUE TRUE .... etc where it’s TRUE when temps[i] was >= 0.5 and FALSE otherwise.

    To rephrase your problem then, we just need to look for occurences of at least five TRUE in a row.

    To do this we can use the function rle. ?rle gives:

    > ?rle
    Description
         Compute the lengths and values of runs of equal values in a vector
         - or the reverse operation.
    Value:
         ‘rle()’ returns an object of class ‘"rle"’ which is a list with
         components:    
     lengths: an integer vector containing the length of each run.
      values: a vector of the same length as ‘lengths’ with the
              corresponding values.
    

    So we use rle which counts up all the streaks of consecutive TRUE in a row and consecutive FALSE in a row, and look for at least 5 TRUE in a row.

    I’ll just make up some data to demonstrate:

    # for you, temps <- Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain
    temps <- runif(1000) 
    
    # make a vector that is TRUE when temperature is >= 0.5 and FALSE otherwise
    ofinterest <- temps >= 0.5
    
    # count up the runs of TRUEs and FALSEs using rle:
    runs <- rle(ofinterest) 
    
    # we need to find points where runs$lengths >= 5 (ie more than 5 in a row), 
    # AND runs$values is TRUE (so more than 5 'TRUE's in a row).
    streakIs <- which(runs$lengths>=5 & runs$values)
    
    # these are all the el_nino occurences. 
    # We need to convert `streakIs` into indices into our original `temps` vector.
    # To do this we add up all the `runs$lengths` up to `streakIs[i]` and that gives
    #  the index into `temps`.
    # that is:
    # startMonths <- c()
    # for ( n in streakIs ) {
    #     startMonths <- c(startMonths,   sum(runs$lengths[1:(n-1)]) + 1
    # }
    #
    # However, since this is R we can vectorise with sapply:
    startMonths <- sapply(streakIs, function(n) sum(runs$lengths[1:(n-1)])+1)
    

    Now if you do Nino3.4_1974_2000$Month_common[startMonths] you’ll get all the months in which the El Nino started.

    It boils down to just a few lines:

    runs <- rle(Nino3.4_1974_2000$Nino3.4_degree_1974_2000_plain>=0.5) 
    streakIs <- which(runs$lengths>=5 & runs$values)
    startMonths <- sapply(streakIs, function(n) sum(runs$lengths[1:(n-1)])+1)
    Nino3.4_1974_2000$Month_common[startMonths]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never developed for WAMP, the hosting guy for my new client says
I am a new guy in Ruby, and I have tables with these primary
So I am an old web forms guy and new to the MVC (in
new Date(05-MAY-09 03.55.50) is any thing wrong with this ? i am getting illegalArgumentException
The plone guy is on leave, I have a request to add breadcrumbs to
I have 3 columns in a DataTable Id Name Count 1 James 4345 2
I`m a new guy to c# and I try to add a some code
We're beginning to design a new product, and our UI guy is knocking out
I'm a new Ruby/Rails guy. Here's one question puzzling me: Can we find the
I'm new to PHP and this question seems stupid. But I'm really confused with

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.