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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:02:32+00:00 2026-06-07T13:02:32+00:00

I have a data frame with a date column and some other value columns.

  • 0

I have a data frame with a date column and some other value columns. I would like to extract from the data frame those rows in which the date column matches any of the elements in a pre-existing list of dates. For example, using a list of one element, the date ‘2012-01-01’ would pull the row with a date of ‘2012-01-01’ from the data frame.

For numbers I think I know how to match the values. This code:

testdf <- data.frame(mydate = seq(as.Date('2012-01-01'), 
                                  as.Date('2012-01-10'), by = 'day'),
                     col1 = 1:10,
                     col2 = 11:20,
                     col3 = 21:30)

…produces this data frame:

       mydate col1 col2 col3
1  2012-01-01    1   11   21
2  2012-01-02    2   12   22
3  2012-01-03    3   13   23
4  2012-01-04    4   14   24
5  2012-01-05    5   15   25
6  2012-01-06    6   16   26
7  2012-01-07    7   17   27
8  2012-01-08    8   18   28
9  2012-01-09    9   19   29
10 2012-01-10   10   20   30

I can do this:

testdf[which(testdf$col3 %in% c('25','29')),]

which produces this:

      mydate col1 col2 col3
5 2012-01-05    5   15   25
9 2012-01-09    9   19   29

I can generalise this to a list like this:

myvalues <- c('25','29')
testdf[which(testdf$col3 %in% myvalues),]

And I get the same output. So I had thought I would be able to use the same approach for dates, but it appears that I was wrong. Doing this:

testdf[which(testdf$mydate %in% c('2012-01-05','2012-01-09')),]

Gets me this:

[1] mydate col1   col2   col3  
<0 rows> (or 0-length row.names)

And popping the dates in their own list – which is the ultimate aim – doesn’t help either. I can think of ways round this with loops or an apply function, but it seems to me that there must be a simpler way for what is probably a fairly common requirement. Is it that I have again overlooked something simple?

Q: How can I subset those rows of a data frame that have a date column the values of which match one of a list of dates?

  • 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-06-07T13:02:34+00:00Added an answer on June 7, 2026 at 1:02 pm

    You have to convert the date string into a Date variable using as.Date (try ?as.Date at the console). Bonus: you can drop which:

    > testdf[testdf$mydate %in% as.Date(c('2012-01-05', '2012-01-09')),]
          mydate col1 col2 col3
    5 2012-01-05    5   15   25
    9 2012-01-09    9   19   29
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data from log files and would like to group entries by
I have a date column in a data frame in chr format as follows:
I have a data frame with two columns. First column contains categories such as
Suppose I have a date.frame like: df <- data.frame(a=1:5, b=sample(1:5, 5, replace=TRUE), c=5:1) df
I have a data frame with a character column of date-times. When I use
I am trying to extract numeric data from a database where the columns have
If I have data like this DF <- data.frame( date = seq(Sys.Date()-1000, len=1000, by=1
I have a data frame that looks like this: site date var dil 1
I have a data frame of daily data: df with four columns: Date, A,
I have some irregular data in one column. my.table <-read.table(text=ticker,date,last a,12/20/2011,289295 a,12/21/2011,NA a,12/27/2011,297001 a,12/28/2011,NA

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.