0
$\begingroup$

i want to convert the whole column from a factor to date.

The str of the dataset I am using is given below:

> str(dataset)
'data.frame':   2538 obs. of  5 variables:
 $ X        : int  1 2 3 4 5 6 7 8 9 10 ...
 $ SessionID: int  13307 21076 27813 8398 23118 12256 28799 11457 7542 19261 ...
 $ Timestamp: Factor w/ 2532 levels "2014-04-01T03:02:33.088Z",..: 2064 905 1086 1027 2419 1327 2035 1206 481 1354 ...
 $ ItemID   : int  214684513 214718203 214716928 214826900 214838180 214717318 214821307 214537967 214835775 214706432 ...
 $ Price    : int  0 0 0 0 0 0 0 0 0 0 ...

How can I convert timestamp variable from factor to specific date format:

YYYY-MM-DDThh:mm:ss.SSSZ (2014-04-06T18:42:05.823Z)

$\endgroup$

1 Answer 1

0
$\begingroup$

One way to do this is to use the PIPE operator to pass the column of the dataframe into your function, then assign it back to the dataframe.

I created a dataframe with one column holding your example date string five times:

> x <- "2014-04-06T18:42:05.823Z"
> DF <- as.data.frame(c(x, x, x, x, x), colnames="original")
> colnames(DF) <- "original"
> str(DF)
'data.frame':   5 obs. of  1 variable:
 $ original: chr  "2014-04-06T18:42:05.823Z" "2014-04-06T18:42:05.823Z" 

I use this PIPE symbol, which allows you to chain methods together, passing the output from one into the input of the following. Using a . symbolises the output from the previous method.

To make the PIPE operator available, load in the magrittr package (I believe it is also included in the tidyverse, so perhaps via the dplyr package:

library(maggritr)

Now I apply the as.POSIXct function to the original column, and save it back to the dataframe in a columnn called new:

DF$new <- DF$original %>% as.POSIXct(., tz = "UTC", "%Y-%m-%dT%H:%M:%OS")
> DF
                  original                     new
1 2014-04-06T18:42:05.823Z 2014-04-06 18:42:05.822
2 2014-04-06T18:42:05.823Z 2014-04-06 18:42:05.822
3 2014-04-06T18:42:05.823Z 2014-04-06 18:42:05.822
4 2014-04-06T18:42:05.823Z 2014-04-06 18:42:05.822
5 2014-04-06T18:42:05.823Z 2014-04-06 18:42:05.822

> str(DF)
'data.frame':   5 obs. of  2 variables:
 $ original: chr  "2014-04-06T18:42:05.823Z" "2014-04-06T18:42:05.823Z" ...
 $ new     : POSIXct, format: "2014-04-06 18:42:05.822" "2014-04-06 18:42:05.822" ...

You can remove the original column if you like, using:

df$original <- NULL
$\endgroup$
3
  • $\begingroup$ after applying this code when i display str(df) the got same result Timestamp with factor :( str(df) 'data.frame': 2538 obs. of 5 variables: $ X : int 1 2 3 4 5 ... $ SessionID: int 13307 21076 27813 8398 23118 ... $ Timestamp: Factor w/ 2532 levels "2014-04-01T03:02:33.088Z",..: 2064 905 1086 1027 2419 1327 2035 1206 481 1354 ... $ ItemID : int 214684513 214718203 214716928 214717318 214821307 214537967 214835775 ... $ Price : int 0 0 0 0 0 0 ... $\endgroup$
    – maira khan
    Commented Aug 6, 2018 at 15:02
  • $\begingroup$ i want output like this > str(dataset) 'data.frame': 2538 obs. of 5 variables: $ X : int 1 2 3 4 5 6 7 8 9 10 ... $ SessionID: int 13307 21076 27813 8398 23118 12256 28799 11457 7542 19261 ... $ Timestamp: Date "2014-04-01T03:02:33.088Z",..: 2064 905 1086 1027 2419 1327 2035 1206 481 1354 ... $ ItemID : int 214684513 214718203 214716928 214826900 214838180 214717318 214821307 214537967 214835775 214706432 ... $ Price : int 0 0 0 0 0 0 0 0 0 0 ... $\endgroup$
    – maira khan
    Commented Aug 6, 2018 at 15:19
  • $\begingroup$ If you try my example with my data, do you get the same output I posted? Otherwise it might be useful to know how you are reading your data and creating the dataframe. Additionally, you could try first converting the factor column to a string column, so it matches my dataframe. $\endgroup$
    – n1k31t4
    Commented Aug 6, 2018 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.