I am trying to convert these dates in created_at column to the number of seconds column created_at_dt using POSIXct.
created_at
<chr>
Fri May 26 17:30:01 0000 2017
Fri May 26 17:30:05 0000 2017
Fri May 26 17:30:05 0000 2017
Fri May 26 17:30:04 0000 2017
Fri May 26 17:30:12 0000 2017
Example of what i want to achieve:
created_at_dt
<dbl>
1495819801
1495819805
1495819805
1495819804
1495819812
I tried the following line but got only NA values introduced.
tweets <- tweets %>%
mutate(created_at_dt = asPOSIXct(as.numeric('created_at')))
Any help would be much appreciated. Thank you!
CodePudding user response:
You just need to specify the correct format string for as.POSIXct
.
Also, created_at
should not be in quotes for mutate()
.
library(dplyr)
tweets <- tweets %>%
mutate(created_at_dt = as.POSIXct(created_at,
format = "%a %B %d %H:%M:%S %z %Y") %>%
as.numeric())
Result:
created_at created_at_dt
1 Fri May 26 17:30:01 0000 2017 1495819801
2 Fri May 26 17:30:05 0000 2017 1495819805
3 Fri May 26 17:30:05 0000 2017 1495819805
4 Fri May 26 17:30:04 0000 2017 1495819804
5 Fri May 26 17:30:12 0000 2017 1495819812
The data:
tweets <- structure(list(created_at = c("Fri May 26 17:30:01 0000 2017",
"Fri May 26 17:30:05 0000 2017", "Fri May 26 17:30:05 0000 2017",
"Fri May 26 17:30:04 0000 2017", "Fri May 26 17:30:12 0000 2017"
)), class = "data.frame", row.names = c(NA, -5L))