R:字符字符串不是标准的明确格式
我想在数据集(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您表达的语法似乎是错误的。用
%&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 dplyrmutate
function is something you are looking for.However in my mind for the stated purpose regex might be a more elegant solution.