如果包含类似
的数据的大型数据集while read oldrev newrev refname;
do
#Do something
mail -s "<Subject>" -r "<From>" "<To>"
done
1)我想将时间小时数据转移到时间
df<- Energy Power StartTime Timehour
7.50 10 2017-10-18 09:47:10.073 0.75
2)然后我想将Time Hour值添加到starttime
df<- Energy Power StartTime Timehour
7.50 10 2017-10-18 09:47:10.073 00:45:00
1)我尝试用Convert time from numeric to time format in R
更改时间表示法 df<- Energy Power StartTime Timehour NewTime
7.50 10 2017-10-18 09:45:10.000 00:45:00 2017-10-18 10:30:10.000
但我收到以下消息
df$Timehour <- as.Date(df$Timehour)
df$Timehour <- format(as.POSIXct(df$Timehour)*86400, origin = "1970-01-01",tz = "UTC", "%H:%M:%OS")
&#39;来源&#39;必须提供
有人可以帮助我解决第一和第二个问题吗?
答案 0 :(得分:1)
基本解决方案:您需要在几秒钟内完成时间,将其简单地添加到Posixct
df$Timehour <- df$Timehour*3600
df$StartTime <- as.POSIXct(as.character(df$StartTime),format = "%Y-%m-%d %H:%M:%OS")
df$newtime <- df$StartTime + df$Timehour
Energy Power StartTime Timehour newtime
1 7.5 10 2017-10-18 09:47:10 2700 2017-10-18 10:32:10
您可以通过这种方式将时间转换为所需的格式:
df$Timehour <- strftime(as.POSIXct(df$Timehour,origin='1900-01-01 00:00'),format="%H:%M:%S",tz = "GMT")
Energy Power StartTime Timehour newtime
1 7.5 10 2017-10-18 09:47:10 00:45:00 2017-10-18 10:32:10
数据:
df<- read.table(text = "Energy, Power, StartTime, Timehour
7.50, 10 , 2017-10-18 09:47:10, 0.75",header = TRUE ,sep = ",")
答案 1 :(得分:0)
如果您使用lubridate将时间小时转换为分钟,则可以将其添加到starttime列
library(lubridate)
library(tidyverse)
library(readr)
df <- read_table("Energy Power StartTime Timehour
7.50 10 2017-10-18 09:47:10.073 0.75")
df %>% mutate(
minutes = minutes(round(Timehour * 60)),
newTime = StartTime + minutes)
# A tibble: 1 x 6
Energy Power StartTime Timehour minutes newTime
<dbl> <int> <dttm> <dbl> <S4: Period> <dttm>
1 7.50 10 2017-10-18 09:47:10 0.750 45M 0S 2017-10-18 10:32:10
如果需要,首先使用
将StartTime列转换为datatime类df <- df %>% mutate(StartTime = ymd_hms(StartTime))