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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:27:36+00:00 2026-06-18T12:27:36+00:00

I am working on a dataframe which I have previously intergrated the time and

  • 0

I am working on a dataframe which I have previously intergrated the time and date into one column (called timestamp):

a <-c(1:21)
D <- c("2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14", "2012/12/14")
Time <- c("18:40:37", "18:40:48", "18:40:58", "18:41:08","18:41:18","18:41:28","18:41:38","18:41:48","18:41:58","18:42:08","18:42:18","18:42:28","18:42:38","18:42:48","18:42:58","18:43:08","18:43:18","18:42:28", "18:44:18", "18:44:28", "18:44:28")
df1 <- data.frame(a, D, Time)
df1 <- within(df1, { timestamp=format(as.POSIXct(paste(D, Time)), "%d/%m/%Y %H:%M:%S") })

How would I subset the dataframe to exclude values after a specific point in time? I found some code in Stackoverflow for a similar question that I thought might be able to help, but I am struggling to get the time element to work:

subset(df1, format.Date(timestamp, ""%d/%m/%Y %H:%M:%S"") >"14/12/2012 18:42:00")

Any advice would be very much appreciated.

Edit:
I am struggling to get the code detailed below working on my real data. A dput() of the first four rows of my dataframe are listed at the end of this post. I previously used the line of code recommended by @Arun to timestamp my data.

gps <- within(gps, { timestamp=format(as.POSIXct(paste(LOCAL.DATE, LOCAL.TIME)), 
+                                       "%d/%m/%Y %H:%M:%S") })

If I try and apply the second part of the code (strptime…) I get the error message:
Error in $<-.data.frame(*tmp*, “timestamp”, value = list(sec = c(37, :
replacement has 30208 rows, data has 4
This sort of explains when I try and appy the code to the whole of my data I get 8 rows of many numbers, separated by a comma. If you can help me in any way, I would be extremely grateful.

structure(list(timestamp = c("14/12/2012 18:40:37", "14/12/2012 18:40:48", 
"14/12/2012 18:40:58", "14/12/2012 18:41:08"), LATITUDE = c(54.77769505, 
54.77765729, 54.77768751, 54.7777021), LONGITUDE = c(-1.56627049, 
-1.56639255, -1.56626555, -1.56662523), HEIGHT = c(" 173.911 M", 
" 161.742 M", " 146.905 M", " 138.016 M"), SPEED = c(" 0.465 km/h", 
" 0.728 km/h", " 4.574 km/h", " 17.335 km/h")), .Names = c("timestamp", 
"LATITUDE", "LONGITUDE", "HEIGHT", "SPEED"), row.names = c(NA, 
4L), class = "data.frame")

Second edit: Many thanks @Arun for the solution. I was a bit confused how I was suppose to use the code as my data is originally in date and time columns (LOCAL.DATE and LOCAL.TIME). So I used the first line of code from your orginal solution, and then the second line from your revised edits.

This is the code I used:

gps <- within(gps, { timestamp=format(as.POSIXct(paste(LOCAL.DATE, LOCAL.TIME)), 
                                      "%d/%m/%Y %H:%M:%S") })

gps$timestamp <- strptime(gps$timestamp, "%Y-%m-%d %H:%M:%S")

However now I get a string of NAs (and some -1s). Apologies if I used the code in the incorrect way…

Third edit
Apologies for the confusion @Arun. When I try it both ways round for the date column, I get errors. If I keep it as yr/m/d, how the original data was formatted, I get dput() of :

structure(list(timestamp = c("2012/12/14 18:40:37", "2012/12/14 18:40:48", 
"2012/12/14 18:40:58", "2012/12/14 18:41:08"), LATITUDE = c(54.77769505, 
54.77765729, 54.77768751, 54.7777021), LONGITUDE = c(-1.56627049, 
-1.56639255, -1.56626555, -1.56662523), HEIGHT = c(" 173.911 M", 
" 161.742 M", " 146.905 M", " 138.016 M"), SPEED = c(" 0.465 km/h", 
" 0.728 km/h", " 4.574 km/h", " 17.335 km/h")), .Names = c("timestamp", 
"LATITUDE", "LONGITUDE", "HEIGHT", "SPEED"), row.names = c(NA, 
4L), class = "data.frame")

If I then use:

gps2$timestamp <- strptime(gps2$timestamp, "%Y/%m/%d %H:%M:%S")

… and try to view the dataframe in R Studio’s workspace window, the R session aborts.

  • 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-18T12:27:38+00:00Added an answer on June 18, 2026 at 12:27 pm

    Its better to load character vectors as such and not factors using stringsAsFactors = FALSE (as shown below)

    # make sure character columns are not converted to factors
    df1 <- data.frame(a, D, Time, stringsAsFactors = FALSE)
    

    Then,

    df1 <- within(df1, { timestamp=format(as.POSIXct(paste(D, Time)), 
                                   "%d/%m/%Y %H:%M:%S") })
    # convert timestamp here
    df1$timestamp <- strptime(df1$timestamp, "%d/%m/%Y %H:%M:%S")
    

    Now, try subset this way:

    # now subset
    subset(df1, timestamp > strptime("14/12/2012 18:42:00", "%d/%m/%Y %H:%M:%S"))
    
    
    #     a          D     Time           timestamp
    # 10 10 2012/12/14 18:42:08 2012-12-14 18:42:08
    # 11 11 2012/12/14 18:42:18 2012-12-14 18:42:18
    # 12 12 2012/12/14 18:42:28 2012-12-14 18:42:28
    # 13 13 2012/12/14 18:42:38 2012-12-14 18:42:38
    # 14 14 2012/12/14 18:42:48 2012-12-14 18:42:48
    # 15 15 2012/12/14 18:42:58 2012-12-14 18:42:58
    # 16 16 2012/12/14 18:43:08 2012-12-14 18:43:08
    # 17 17 2012/12/14 18:43:18 2012-12-14 18:43:18
    # 18 18 2012/12/14 18:42:28 2012-12-14 18:42:28
    # 19 19 2012/12/14 18:44:18 2012-12-14 18:44:18
    # 20 20 2012/12/14 18:44:28 2012-12-14 18:44:28
    # 21 21 2012/12/14 18:44:28 2012-12-14 18:44:28
    

    Edit: Try this:

    df1 <- within(df1, { timestamp=as.POSIXct(timestamp, format = "%d/%m/%Y %H:%M:%S") })
    df1$timestamp <- strptime(df1$timestamp, "%Y-%m-%d %H:%M:%S")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to standardize all except one column in a dataframe, with which I'm
I now have a small java script server working correctly, called by: <?php $handle
I'm having a dataframe as like below. > avg_data date region AveElapsedTime 1 5/1/2012
I am currently working on two different dataframes, one of which is extremely long
I am working on a large dataframe with >40 columns. I want to be
I am working with a data frame where one of the columns consists of
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
Working with MS Access for the first time and coming across a few problems
I'm working with ggplot2 to generate some geom_line plots which i've already generated from
I'm working with a large data frame, and have run up against RAM limits.

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.