转换“字符” “ posix”在r
我是R的新手R,并试图在R中复制我当前的Excel报告。我有一个多列CSV文件,其中包含“ 4/18/2022,2:15 pm”格式的列的列(直接复制)来自数据文件)。最终,我需要在CSV中提取每行的一周和一天号。
我已经将字符转换为Posix了。
datetest = ("4/18/2022, 12:53:22 AM")
datetest1 <- mdy_hms(datetest)
print(datetest1)
sapply(datetest1, class)
这产生了
> datetest = ("4/18/2022, 12:53:22 AM")
> datetest1 <- mdy_hms(datetest)
> print(datetest1)
[1] "2022-04-18 00:53:22 UTC"
> sapply(datetest1, class)
[,1]
[1,] "POSIXct"
[2,] "POSIXt"
,但我的问题是当我尝试转换整列时
#import CSV
RawData <- read.csv("data.csv",header = TRUE, ",")
#convert TS to POSIX
#column header is Date.Time.Opened
escRawData[['Date.Time.Opened']] <- mdy_hms(escRawData[['Date.Time.Opened']])
,这给了我这个
> escRawData <-read.csv("report1650637729324.csv",header = TRUE, ",")
> #convert TS to POSIX
> escRawData[['Date.Time.Opened']] <- mdy_hms(escRawData[['Date.Time.Opened']])
Warning message:
All formats failed to parse. No formats found.
清除了数据框的所有数据。我用Sapply检查了CSV中的数据类型,并且我知道数据是“字符”。
我已经阅读/观看了许多描述润滑脂的教程,并且Posixlt命令都给出了或多或少的“未找到格式”的错误,因此很明显,我缺少一些基本的东西。任何帮助将不胜感激。
I'm new to R and trying to replicate my current excel reports in R. I have a multi-column csv file with a column containing a timestamp in the format of "4/18/2022, 2:15 PM" (copied directly from the datafile). Ultimately I need to extract the week number and day number for each row in the csv.
I've gotten this far in converting the char to POSIX.
datetest = ("4/18/2022, 12:53:22 AM")
datetest1 <- mdy_hms(datetest)
print(datetest1)
sapply(datetest1, class)
which produces
> datetest = ("4/18/2022, 12:53:22 AM")
> datetest1 <- mdy_hms(datetest)
> print(datetest1)
[1] "2022-04-18 00:53:22 UTC"
> sapply(datetest1, class)
[,1]
[1,] "POSIXct"
[2,] "POSIXt"
But my problem is when I try to convert the entire column
#import CSV
RawData <- read.csv("data.csv",header = TRUE, ",")
#convert TS to POSIX
#column header is Date.Time.Opened
escRawData[['Date.Time.Opened']] <- mdy_hms(escRawData[['Date.Time.Opened']])
which gives me this
> escRawData <-read.csv("report1650637729324.csv",header = TRUE, ",")
> #convert TS to POSIX
> escRawData[['Date.Time.Opened']] <- mdy_hms(escRawData[['Date.Time.Opened']])
Warning message:
All formats failed to parse. No formats found.
which has cleared all the data from the dataframe. I used sapply to check the datatype in the CSV and I know that the data is "character."
I've read/watched a number of tutorials describing lubridate and the POSIXlt commands all giving more or less the same error of "no formats found" so obviously, I am missing something basic. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用您当前的代码,一个错误的日期条目(以格式化或其他)打破了整列的转换。您可以事先检查原始数据,也可以转换它们 rowwise 并检查失败的转换( na s),例如:
With your current code, one faulty date entry (format-wise or other) breaks the whole column's conversion. You could inspect the raw data beforehand, or transform them rowwise and check for failed conversions (NAs) like:
好的,大家,我发现我做错了什么。 @ku99的评论帮助我弄清楚了。我的实际数据文件不包括时间戳中的秒。当我更改为使用
它时,它可以很好地工作。
Ok Everyone, I found out what I was doing wrong. @KU99's comment helped me figure it out. My actual data file did not include seconds in the time stamp. When I changed to using
it worked perfectly.