我正在尝试将我的日期数据集从chr转换为时间格式。
这是我的代码:
time<-f1%>%
mutate(`ymd_hms(Timestamp)`=strftime(f1$`ymd_hms(Timestamp)`, tz="GMT", format = "%H:%M:%S"))%>%
transform(as.numeric(f1$SpotPrice))
chron(times=f1$`ymd_hms(Timestamp)`)它的显示错误:
Error in chron(., times = f1$`ymd_hms(Timestamp)`) :
. and f1$`ymd_hms(Timestamp)` must have equal lengths如果我评论一下
time<-f1%>%
mutate(`ymd_hms(Timestamp)`=strftime(f1$`ymd_hms(Timestamp)`, tz="GMT", format = "%H:%M:%S"))%>%
transform(as.numeric(f1$SpotPrice))
#chron(times=f1$`ymd_hms(Timestamp)`)输出是
'data.frame': 10078 obs. of 4 variables:
$ InstanceType : chr " a1.2xlarge" " a1.2xlarge" " a1.2xlarge" " a1.4xlarge" ...
$ ProductDescription: chr " Linux/UNIX" " Red Hat Enterprise Linux" " SUSE Linux" " Linux/UNIX" ...
$ SpotPrice : num 0.0671 0.1971 0.2171 0.1343 0.2643 ...
$ ymd_hms(Timestamp): chr "06:17:23" "06:17:23" "06:17:23" "12:15:54" ...此外,我还将此推荐作为一个单一变量,如下所示:
f2<-chron(times=DfUse$`ymd_hms(Timestamp)`)它工作并显示输出。
'times' num [1:10078] 06:17:23 06:17:23 06:17:23 12:15:54 12:15:54 ...
- attr(*, "format")= chr "h:m:s"`但是,我希望将其与整个数据集一起使用,而不是将其单独用作一个变量。
提前谢谢。
发布于 2021-07-12 13:12:13
语法错误很少,代码中也有改进的余地。
ymd_hms(Timestamp) =,但您不需要这样做,因为它创建了一个名为ymd_hms(Timestamp) =的列。只使用Timestamp应该是好的。dplyr管道中使用$。dplyr函数分开。transform位于基R中,但您可以使用相同的mutate管道将SpotPrice转换为数字.library(dplyr)
time<-f1%>%
mutate(Timestamp = strftime(ymd_hms(Timestamp), tz="GMT", format = "%H:%M:%S"),
SpotPrice = as.numeric(SpotPrice),
Timestamp = chron::chron(times=Timestamp))https://stackoverflow.com/questions/68347925
复制相似问题