0

I've got a section of code, let's assume it's

x <- c("10/05/1997 00:00:00", "11/05/1997 00:00:00", "12/05/1997 00:00:00")  
x <- strsplit(as.character(x), " ", fixed=TRUE)[1]

The issue I'm running into is this: I want to take the first index of the split string ("10/05/1997") while discarding the second index of the split string ("00:00:00"). However, rather than indexing through the split string, I'm currently telling R to only perform this operation on the first index of x. I would have thought in order to only perform this on the first index of x, my code would have to look like this:

x <- strsplit(as.character(x)[1], " ", fixed=TRUE)[1]

Is there a way to pull just the first element of the split string for each index in the vector?

Thanks for the help, all. I'm very much an R newbie. I couldn't find any similar issues.

Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56
  • Are you looking for this? `lapply(x,function(y) y[1])`. Also if the goal is to get a date in `mm/dd/yyyy` format, P Lapointe's solution is probably best – Mike H. Jun 30 '17 at 17:33
  • The goal at the moment is to separate date and time so I can process them separately. They need to end up in the format yyyyjjj.iii where j is the number of day in the year and i is the decimal value of the time of day (12:00 would be .5). I'll need the time at some point, just not yet. – Daniel Wilkerson Jun 30 '17 at 18:03

1 Answers1

0

Simply using as.Date will get rid of the time component:

x <- c("10/05/1997 00:00:00", "11/05/1997 00:00:00", "12/05/1997 00:00:00")  
as.Date(x,"%m/%d/%Y")

"1997-10-05" "1997-11-05" "1997-12-05"
Pierre Lapointe
  • 16,017
  • 2
  • 43
  • 56
  • Thanks! This definitely helps with this section. I'll need the time later, however. Is there a way to strip time out in the same way? – Daniel Wilkerson Jun 30 '17 at 18:04
  • @DanielWilkerson Usually, it's better to keep the date when doing operations on time. For example, if you want to know how many hours from 23:00 to 1:00 the next day, R has to know it's not the same date. For date_time objects, the classic is `as.POSIXct`. Also, look at package `lubridate`, which is very good to handle all sorts of date. You mentioned decimal date: the package has a `decimal_date` function, which also handles time. – Pierre Lapointe Jun 30 '17 at 18:14
  • Thanks for the response! I'll definitely dig into the packages you mentioned. The concern at the moment is that I'm prepping data to go into two different models and the model expects dates in a strange format. – Daniel Wilkerson Jun 30 '17 at 18:21