这是我正在使用的数据框。我只包括前六行。我收到的错误消息与数据的这一较小部分相同。
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")
我正在使用 lubritation
库,并且正在尝试将A'M/d/y HMS格式(Excel)(Excel)转换为“ YMD”格式。
library(lubridate)
我正在尝试创建一个新的变量,即YMD格式,我指定Lubridate正在查看MDY HMS。
file$Date.time2 <- lubridate::ymd(file$Date.time, "mdy_hms")
但是,我收到此错误消息。
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
我查看此数据框架的长度,正如我期望的那样,它确实有六行,因为我使用了 head()
函数。
length(file$Date.time) # evaluates to 6
我还可以确认该文件的类是一个数据框
class(file) # dataframe
,所有数据的全范围都有8077列,而lubridate则告诉我相同的错误消息,说替换为8078行。
我尝试以这两种方式运行代码,以为如果我使用新向量而不是同一矢量,则可能会消失。
file$Date.time <- lubridate::ymd(file$Date.time, "mdy_hms")
file$Date.time2 <- lubridate::ymd(file$Date.time, "mdy_hms")
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 `lt;-.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")
发布评论
评论(1)
列表的字符串中没有几秒钟,因此您需要在没有几秒钟的情况下使用橄榄酸功能:
这是一个有用的作弊表:https://rawgit.com/rstudio/cheatsheets/main/lubridate.pdf
There are no seconds in the list's strings, so you need to use the lubridate function without seconds in it:
Here's a useful cheatsheet with these functions: https://rawgit.com/rstudio/cheatsheets/main/lubridate.pdf