R:字符字符串不是标准的明确格式

发布于 2025-01-21 17:15:28 字数 941 浏览 4 评论 0原文

我想在数据集(UFO)中有一个列(DateTime),我想将其分为一天,月,年,Dayofweek,小时和分钟。通过这样做,我会收到错误:as.posixlt.character(x,tz = tz(x))中的错误:字符串不采用标准的明确格式。

DateTime专栏看起来像这样:

DateTime

1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1:

00

21/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/1/12:

00

是我的代码:

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 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?

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

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

发布评论

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

评论(1

你在我安 2025-01-28 17:15:29

您表达的语法似乎是错误的。用%&gt;%运算符构建的管道意味着将上一个(左侧)表达式的结果传递给第二个(右侧)表达式的动词(函数)作为第一个参数。您的代码是一系列作业而不是函数。我猜dplyr 突变函数是您要寻找的东西。

然而,在我看来,陈述的目的正则是更优雅的解决方案。

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.

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