日期和时间默认为1月1日,在卢比特R软件包中的1AD,1AD

发布于 2025-02-08 12:11:47 字数 1316 浏览 2 评论 0原文

伙计们...

我在橄榄酸酯中正确出现的日期/时间遇到了麻烦。

这是我的代码:

Temp.dat <- read_excel("Temperature Data.xlsx", sheet = "Sheet1", na="NA") %>%
  mutate(Treatment = as.factor(Treatment),
         TempC=as.factor(TempC),
         TempF=as.factor(TempF),
         Month=as.factor(Month),
         Day=as.factor(Day),
         Year=as.factor(Year),
         Time=as.factor(Time))%>%
 select(TempC, Treatment, Month, Day, Year, Time)%>%
 mutate(Measurement=make_datetime(Month, Day, Year, Time))

这就是它吐出的:

tibble [44 x 7] (S3: tbl_df/tbl/data.frame)
 $ TempC      : Factor w/ 38 levels "15.5555555555556",..: 31 32 29 20 17 28 27 26 23 24 ...
 $ Treatment  : Factor w/ 2 levels "Grass","Soil": 1 1 1 1 2 2 2 2 2 2 ...
 $ Month      : Factor w/ 1 level "6": 1 1 1 1 1 1 1 1 1 1 ...
 $ Day        : Factor w/ 2 levels "15","16": 1 1 1 1 1 1 1 1 1 1 ...
 $ Year       : Factor w/ 1 level "2022": 1 1 1 1 1 1 1 1 1 1 ...
 $ Time       : Factor w/ 3 levels "700","1200","1600": 3 3 3 3 3 3 3 3 3 3 ...
 **$ Measurement: POSIXct[1:44], format: "0001-01-01 03:00:00" "0001-01-01 03:00:00" "0001-01-01 03:00:00" "0001-01-01 03:00:00" ...**

我已经根据问题结果放了星号。它应该在6月16日在0700或类似的东西吐出,但由于某种原因,它默认为1月1日,1AD。我已经尝试在Excel的日期中添加结肠,但是默认为12小时的时间,我想将其保留在24小时。

这是怎么回事?

folks...

I am having trouble with date/time showing up properly in lubridate.

Here's my code:

Temp.dat <- read_excel("Temperature Data.xlsx", sheet = "Sheet1", na="NA") %>%
  mutate(Treatment = as.factor(Treatment),
         TempC=as.factor(TempC),
         TempF=as.factor(TempF),
         Month=as.factor(Month),
         Day=as.factor(Day),
         Year=as.factor(Year),
         Time=as.factor(Time))%>%
 select(TempC, Treatment, Month, Day, Year, Time)%>%
 mutate(Measurement=make_datetime(Month, Day, Year, Time))

Here's what it spits out:

tibble [44 x 7] (S3: tbl_df/tbl/data.frame)
 $ TempC      : Factor w/ 38 levels "15.5555555555556",..: 31 32 29 20 17 28 27 26 23 24 ...
 $ Treatment  : Factor w/ 2 levels "Grass","Soil": 1 1 1 1 2 2 2 2 2 2 ...
 $ Month      : Factor w/ 1 level "6": 1 1 1 1 1 1 1 1 1 1 ...
 $ Day        : Factor w/ 2 levels "15","16": 1 1 1 1 1 1 1 1 1 1 ...
 $ Year       : Factor w/ 1 level "2022": 1 1 1 1 1 1 1 1 1 1 ...
 $ Time       : Factor w/ 3 levels "700","1200","1600": 3 3 3 3 3 3 3 3 3 3 ...
 **$ Measurement: POSIXct[1:44], format: "0001-01-01 03:00:00" "0001-01-01 03:00:00" "0001-01-01 03:00:00" "0001-01-01 03:00:00" ...**

I've put asterisks by the problem result. It should spit out June 16th at 0700 or something like that, but instead it's defaulting to January 01, 1AD for some reason. I've tried adding colons to the date in excel, but that defaults to a 12-hour timecycle and I'd like to keep this at 24 hours.

What's going on here?

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

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

发布评论

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

评论(1

葬花如无物 2025-02-15 12:11:47

只要将日期的excel文件中的格式设置为时间,并且它将作为日期时间对象导入lubridate可以解释的日期时间对象。

library(dplyr)
library(lubridate)
Temp.dat <- read_excel("t.xlsx", sheet = "Sheet1", na="NA") %>%
  mutate(Treatment = as.factor(Treatment),
         TempC = as.numeric(TempC),
         TempF = as.numeric(TempF),
         Month = as.numeric(Month),
         Day = as.numeric(Day),
         Year = as.numeric(Year),
         Hour = hour(Time),
         Minute = minute(Time)) %>%
  select(TempC, Treatment, Month, Day, Year, Hour, Minute) %>%
  mutate(Measurement = make_datetime(year = Year, 
                                     month = Month, 
                                     day = Day, 
                                     hour = Hour,
                                     min = Minute))

请注意,make_dateTime()的参数值设置为数字,这是该功能所期望的。如果通过因素,该功能会为您提供您所看到的奇怪日期。

正如我在评论中所建议的那样,无需将时间转换为字符串和提取小时和分钟,因为您可以使用lubridate的minute() and hour()< /代码>功能。

编辑
为了能够使用lubridate的功能时间需要是日期时间对象。您可以通过查看read_excel()产生的内容来检查它

> str(read_excel("t.xlsx", sheet = "Sheet1", na="NA"))
tibble [2 × 7] (S3: tbl_df/tbl/data.frame)                                                     
 $ Treatment: chr [1:2] "s" "c"
 $ TempC    : num [1:2] 34 23
 $ TempF    : num [1:2] 99 60
 $ Month    : num [1:2] 5 4
 $ Day      : num [1:2] 1 15
 $ Year     : num [1:2] 2020 2021
 $ Time     : POSIXct[1:2], format: "1899-12-31 04:33:23" "1899-12-31 03:20:23"

是否参见time是类型posixct,日期时间对象。如果不是这样,则需要将其转换为一个,如果要使用lubridate的minute()hour()函数。如果无法转换,则还有其他解决方案,但它们取决于您拥有的解决方案。

This will work as long as the format in the excel file for date is set to time, and it imports as a date-time object that lubridate can interpret.

library(dplyr)
library(lubridate)
Temp.dat <- read_excel("t.xlsx", sheet = "Sheet1", na="NA") %>%
  mutate(Treatment = as.factor(Treatment),
         TempC = as.numeric(TempC),
         TempF = as.numeric(TempF),
         Month = as.numeric(Month),
         Day = as.numeric(Day),
         Year = as.numeric(Year),
         Hour = hour(Time),
         Minute = minute(Time)) %>%
  select(TempC, Treatment, Month, Day, Year, Hour, Minute) %>%
  mutate(Measurement = make_datetime(year = Year, 
                                     month = Month, 
                                     day = Day, 
                                     hour = Hour,
                                     min = Minute))

Notice the value for the arguments for make_datetime() are set to numeric, which is what the function expects. If you pass factors, the function gives you the weird dates you were seeing.

No need to convert Time to string and extract hours and minutes, as I suggested in the comments, since you can use lubridate's minute() and hour() functions.

EDIT
In order to be able to use lubridate's functions Time needs to be a date-time object. You can check that it is by looking at what read_excel() produces

> str(read_excel("t.xlsx", sheet = "Sheet1", na="NA"))
tibble [2 × 7] (S3: tbl_df/tbl/data.frame)                                                     
 $ Treatment: chr [1:2] "s" "c"
 $ TempC    : num [1:2] 34 23
 $ TempF    : num [1:2] 99 60
 $ Month    : num [1:2] 5 4
 $ Day      : num [1:2] 1 15
 $ Year     : num [1:2] 2020 2021
 $ Time     : POSIXct[1:2], format: "1899-12-31 04:33:23" "1899-12-31 03:20:23"

See that Time is type POSIXct, a date-time object. If it is not, then you need to convert it into one if you want to use lubridate's minute() and hour() functions. If it cannot be converted, there are other solutions, but they depend on what you have.

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