'R: character string is not in a standard unambiguous format

I have a column (datetime) in a dataset (ufo) that I would like to split into day, month, year, dayofweek, hour, and minute. By doing so, I am getting the error: Error in as.POSIXlt.character(x, tz = tz(x)) : character string is not in a standard unambiguous format.

The datetime column looks like this:

datetime

1/1/1910 24:00

1/1/1944 12:00

1/1/1956 05:30

1/1/1957 21:00

1/1/1958 22:00

This is my code:

library(lubridate)

ufo <- ufo%>%
  ufo$datetime <- as.numeric(as.character(ufo$datetime)) %>%
  ufo$datetime <- as.POSIXct(strptime(ufo$datetime,format="%m/%d/%Y %H:%M")) %>%
  ufo$day <- factor(day(ufo$datetime))%>%
  ufo$month <- factor(month(ufo$datetime, label = TRUE))%>%
  ufo$year <- factor(year(ufo$datetime))%>%
  ufo$dayofweek <- factor(wday(ufo$datetime, label = TRUE))%>%
  ufo$hour <- factor(hour(ufo$datetime))%>%
  ufo$minute <- factor(minute(ufo$datetime))%>%

I converted the vector to numeric in the first line of code, and still getting this error. What am I doing wrong?



Solution 1:[1]

The syntax of your expression seems to be wrong. The pipeline built with %>% operator means theat the result of the previous (left side) expression is passed to the verb (function) of the second (right side) expression as the first parameter. Your code is a sequence of assignments instead of functions. I guess dplyr mutate function is something you are looking for.

However in my mind for the stated purpose regex might be a more elegant solution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1