Bram
Bram

Reputation: 342

R integer (date) to number

I have a matrix date that looks like this:

    Date        Time
 1  2017-05-19  08:52:21
 2
 3  2017-05-20  22:29:29
 4  2017-05-20  15:21:35

Both date$Date and date$Time are integers. I would like to obtain a new column like this:

    Date        Time
 1  20170519    085221
 2  NA          NA
 3  20170520    222929
 4  20170520    152135

I've tried with as.character, as.numeric, as.Date... But can't find the solution /=

Sorry if the question was already answer in another post, but I wasn't able to find it!

Upvotes: 0

Views: 130

Answers (2)

4rj4n
4rj4n

Reputation: 356

Since you apparently don't necessarily want date or time class objects (do you?), and since you don't further specify what exactly you need this for, there seems no need to work with date or time functions.
You could try this:

Step 1: First, if you want empty cells to contain NA, fill those in per column

df$Date[df$Date == ""] <- NA
df$Time[df$Time == ""] <- NA

Step 2: And then simply replace the "-" and ":" in the Date and Time values, respectively, to get the wanted strings

df$Date <- gsub(pattern = "-", x = df$Date, replacement = "")
df$Time <- gsub(pattern = ":", x = df$Time, replacement = "")

      Date   Time
1 20170519  85221
2     <NA>   <NA>
3 20170520 222929
4 20170520 152135

The output might not yield integer classes (my starting df resembling your df did not contain integers, so can't double check; result here were character classes), so if you really want integer classes, simply apply as.integer().
As you see the output is the same as your expected output, except for the leading "0" of the row 1 Time value. If need be, there's a work around to get that in there, although I'm not sure what that would add. And after applying as.integer it would most likely disappear anyway.

Upvotes: 0

Andrew Gustar
Andrew Gustar

Reputation: 18425

You need format...

format(as.POSIXct("2017-05-19"),"%Y%m%d")
[1] "20170519"

format(as.POSIXct("08:52:21",format="%H:%M:%S"),"%H%M%S")
[1] "085221"

See ?strptime for the formatting codes.

Upvotes: 1

Related Questions