转换“字符” “ posix”在r

发布于 2025-01-23 08:08:04 字数 1208 浏览 0 评论 0原文

我是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 技术交流群。

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

发布评论

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

评论(2

倾`听者〃 2025-01-30 08:08:04

使用您当前的代码,一个错误的日期条目(以格式化或其他)打破了整列的转换。您可以事先检查原始数据,也可以转换它们 rowwise 并检查失败的转换( na s),例如:

library(dplyr)
library(lubridate)

converted_data <-
    escRawData %>%
      rowwise %>% ## important
      mutate(date_converted = mdy_hms(Date.Time.Opened))

## which rows went wrong?
converted_data %>%
    filter(is.na(date_converted))

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:

library(dplyr)
library(lubridate)

converted_data <-
    escRawData %>%
      rowwise %>% ## important
      mutate(date_converted = mdy_hms(Date.Time.Opened))

## which rows went wrong?
converted_data %>%
    filter(is.na(date_converted))

烂人 2025-01-30 08:08:04

好的,大家,我发现我做错了什么。 @ku99的评论帮助我弄清楚了。我的实际数据文件不包括时间戳中的秒。当我更改为使用

escRawData[['Date.Time.Opened']] <- mdy_hm(escRawData[['Date.Time.Opened']])

它时,它可以很好地工作。

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

escRawData[['Date.Time.Opened']] <- mdy_hm(escRawData[['Date.Time.Opened']])

it worked perfectly.

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