可能使用
hms
包是一个转换选项
time
列来自
character
并存储为
difftime
。
library(hms)
setClass('tagDate')
setClass('tagTime')
setAs("character","tagDate", function(from) as.Date(from, format="%d/%m/%y"))
setAs("character","tagTime", function(from) as.hms(from)) #Changing to difftime
df <- read.csv(textConnection(text),header = FALSE,
colClasses=c('tagDate','tagTime','character','real'))
names(df) <- c('tagDate', 'tagTime', 'tagName', 'tagValue')
head(df)
# tagDate tagTime tagName tagValue
# 1 2018-05-10 00:00:03 SP-PALL-01 0.0500000
# 2 2018-05-10 00:00:03 SP-PDALL-05 0.1000000
# 3 2018-05-10 00:00:03 PT33-PT34 0.2124968
# 4 2018-05-10 00:00:03 PT32-PT36 0.4283838
# 5 2018-05-10 00:00:03 SP-PDAH-03 0.6000000
# 6 2018-05-10 00:00:03 PT32-PT34 0.6058416
# 7 2018-05-10 00:00:03 SP-PDAH-04 0.7000000
# 8 2018-05-10 00:00:03 PT-31 2.4700246
# 9 2018-05-10 00:00:03 PT32-PT31 12.3945660
# 10 2018-05-10 00:00:03 PT-32 14.7820790
数据:
text <- "10/05/18,0:00:03,SP-PALL-01,0.05
10/05/18,0:00:03,SP-PDALL-05,0.1
10/05/18,0:00:03,PT33-PT34,0.21249676
10/05/18,0:00:03,PT32-PT36,0.42838383
10/05/18,0:00:03,SP-PDAH-03,0.6
10/05/18,0:00:03,PT32-PT34,0.60584164
10/05/18,0:00:03,SP-PDAH-04,0.7
10/05/18,0:00:03,PT-31,2.4700246
10/05/18,0:00:03,PT32-PT31,12.394566
10/05/18,0:00:03,PT-32,14.782079"