Home > front end >  R lubridate seeing the wrong number of rows
R lubridate seeing the wrong number of rows

Time:05-13

Here is the dataframe that I am using. I'm only including the first six rows. The error message that I'm receiving is the same with this smaller section of the data.

structure(list(Date.time = c("3/3/22 16:03", "3/4/22 23:41", 
"3/14/22 16:32", "3/23/22 11:44", "3/23/22 13:02", "3/23/22 13:14"
)), row.names = c(NA, 6L), class = "data.frame")

I'm using the lubridate library and I'm trying to convert this column which is an a 'm/d/y hms' format (Excel) into the 'ymd' format that R prefers.

library(lubridate)

I'm trying to create a new variable that is the ymd format and I'm specifying that lubridate is looking at the mdy hms.

file$Date.time2 <- lubridate::ymd(file$Date.time, "mdy_hms")

However, I get this error message.

All formats failed to parse. No formats found.Error in `$<-.data.frame`(`*tmp*`, Date.time2, value = c(NA_real_, NA_real_,  : 
  replacement has 7 rows, data has 6

I look at the length of this data frame and it does have six rows, as I expect, since I used the head() function.

length(file$Date.time) # evaluates to 6

I can also confirm that the class of this file is a dataframe

class(file) # dataframe

The full range of the data had 8077 columns and lubridate is telling is giving me the same error message saying the replacement has 8078 rows.

I tried running the code in both of these ways, thinking that maybe the error message would go away if I used a new vector instead of the same one.

file$Date.time <- lubridate::ymd(file$Date.time, "mdy_hms")
file$Date.time2 <- lubridate::ymd(file$Date.time, "mdy_hms")

CodePudding user response:

There are no seconds in the list's strings, so you need to use the lubridate function without seconds in it:

library(lubridate)

file <- structure(list(Date.time = c("3/3/22 16:03", "3/4/22 23:41", 
"3/14/22 16:32", "3/23/22 11:44", "3/23/22 13:02", "3/23/22 13:14"
)), row.names = c(NA, 6L), class = "data.frame")

file$Date.time2 <- lubridate::mdy_hm(file$Date.time)

Here's a useful cheatsheet with these functions: https://rawgit.com/rstudio/cheatsheets/main/lubridate.pdf

  • Related