日期和时间默认为1月1日,在卢比特R软件包中的1AD,1AD
伙计们...
我在橄榄酸酯中正确出现的日期/时间遇到了麻烦。
这是我的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要将日期的excel文件中的格式设置为时间,并且它将作为日期时间对象导入
lubridate
可以解释的日期时间对象。请注意,
make_dateTime()
的参数值设置为数字,这是该功能所期望的。如果通过因素,该功能会为您提供您所看到的奇怪日期。正如我在评论中所建议的那样,无需将
时间
转换为字符串和提取小时和分钟,因为您可以使用lubridate的minute()
andhour()< /代码>功能。
编辑
为了能够使用lubridate的功能
时间
需要是日期时间对象。您可以通过查看read_excel()
产生的内容来检查它是否参见
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.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'sminute()
andhour()
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 whatread_excel()
producesSee that
Time
is typePOSIXct
, a date-time object. If it is not, then you need to convert it into one if you want to use lubridate'sminute()
andhour()
functions. If it cannot be converted, there are other solutions, but they depend on what you have.