r橄榄酸酯看到错误的行数

发布于 2025-01-28 10:37:16 字数 1196 浏览 4 评论 0 原文

这是我正在使用的数据框。我只包括前六行。我收到的错误消息与数据的这一较小部分相同。

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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青芜 2025-02-04 10:37:16

列表的字符串中没有几秒钟,因此您需要在没有几秒钟的情况下使用橄榄酸功能:

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)

这是一个有用的作弊表: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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文