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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:57:56+00:00 2026-05-27T01:57:56+00:00

I have a questions that might be too basic, but here it is… I

  • 0

I have a questions that might be too basic, but here it is…

I want to extract monthly data from a dataset like this:

    Date     Obs
1 2001-01-01 120
2 2001-01-02 100
3 2001-01-03 150
4 2001-01-04 175
5 2001-01-05 121
6 2001-01-06 100

I just want to get the rows from the data where I have a certain month(e.g. January), this works perfectly:

output=which(strftime(dataset[,1],"%m")=="01",dataset[,1])

However when I try to create a loop to go through all the months using a variable that is declared has character it doesn’t work and I only get “FALSE”.

value=as.character(k)
output=which(strftime(dataset[,1],"%m")==value,dataset[,1])
  • 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-27T01:57:56+00:00Added an answer on May 27, 2026 at 1:57 am

    Do not parse dates as strings. That is too error prone. Parse dates as dates, and do logical comparisons on them.

    Here is one approach, creating January to March data and sub-setting February based on a comparison:

    R> output <- data.frame(date=seq(as.Date("2011-01-01"), by=7, length=10), 
    +                       value=cumsum(runif(10)*100))
    R> output
             date     value
    1  2011-01-01   8.29916
    2  2011-01-08  44.82950
    3  2011-01-15  72.08662
    4  2011-01-22 134.19277
    5  2011-01-29 221.67744
    6  2011-02-05 245.77195
    7  2011-02-12 314.82081
    8  2011-02-19 396.34661
    9  2011-02-26 437.14286
    10 2011-03-05 442.41321
    R> output[ output[,"date"] >= as.Date("2011-02-01") & 
    +          output[,"date"] <= as.Date("2011-02-28"), ]
            date   value
    6 2011-02-05 245.772
    7 2011-02-12 314.821
    8 2011-02-19 396.347
    9 2011-02-26 437.143
    R> 
    

    Another approach uses the xts package:

    R> oo <- xts(output[,"value"], order.by=output[,"date"])
    R> oo
                    [,1]
    2011-01-01   8.29916
    2011-01-08  44.82950
    2011-01-15  72.08662
    2011-01-22 134.19277
    2011-01-29 221.67744
    2011-02-05 245.77195
    2011-02-12 314.82081
    2011-02-19 396.34661
    2011-02-26 437.14286
    2011-03-05 442.41321
    R> oo["2011-02-01::2011-02-28"]
                   [,1]                 
    2011-02-05 245.772
    2011-02-12 314.821
    2011-02-19 396.347
    2011-02-26 437.143
    R> 
    

    as xts has convenient date parsing for the index; see the package documentation for details.

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

Sidebar

Related Questions

There have been a couple of questions that sort of dealt with this but
I have some questions about basic CSS that I was unable to understand or
I am sure that this kind of questions must have been asked before, but
I have some basic questions around understanding fundamentals of Performance testing. I know that
Hopefully someone here might have an answer to my question. I have a basic
Lots of style-related jquery autocomplete questions (yow), but none that want to "match the
All this might look too trivial but read it through - I have simple
I have some basic questions about core data (which I am new to) and
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
This is a little different than the questions that have already been asked on

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.