Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r中的复杂数据处理_R_Date_Time_As.date - Fatal编程技术网

r中的复杂数据处理

r中的复杂数据处理,r,date,time,as.date,R,Date,Time,As.date,我有一个data.frame,有两列。两者,日期为字符: a <- c("01-01-2007 00:00:00", "01-02-2007 00:00:00", "03-05-2007 00:00:00", "31-08-2007 00:00:00") b <- c("01-01-1960 01:25:30", "01-01-1960 1:05:36", "01-01-1960 02:25:59", "01-01-1960 1:20:30") df <- as.data.fr

我有一个data.frame,有两列。两者,日期为字符:

a <- c("01-01-2007 00:00:00", "01-02-2007 00:00:00", "03-05-2007 00:00:00", "31-08-2007 00:00:00")
b <- c("01-01-1960 01:25:30", "01-01-1960 1:05:36", "01-01-1960 02:25:59", "01-01-1960 1:20:30")
df <- as.data.frame(cbind(a,b))
df
                    a                   b
1 01-01-2007 00:00:00 01-01-1960 01:25:30
2 01-02-2007 00:00:00  01-01-1960 1:05:36
3 03-05-2007 00:00:00 01-01-1960 02:25:59
4 31-08-2007 00:00:00  01-01-1960 1:20:30

a尝试使用
lubridate
包装:

library(lubridate)

a <- c("01-01-2007 00:00:00", "01-02-2007 00:00:00", "03-05-2007 00:00:00", "31-08-2007 00:00:00")
b <- c("01-01-1960 01:25:30", "01-01-1960 1:05:36", "01-01-1960 02:25:59", "01-01-1960 1:20:30")
df <- as.data.frame(cbind(a,b))
df

hr <- hour(parse_date_time(b, "dmy HMS"))
minu <- minute(parse_date_time(b, "dmy HMS"))
sec<- second(parse_date_time(b, "dmy HMS"))

getDate <- as_date(parse_date_time(a, "dmy HMS"))
getTime <- paste(hr, minu, sec, sep = ":")

as_datetime(paste(getDate, getTime))
库(lubridate)

a尝试使用
lubridate
套装:

library(lubridate)

a <- c("01-01-2007 00:00:00", "01-02-2007 00:00:00", "03-05-2007 00:00:00", "31-08-2007 00:00:00")
b <- c("01-01-1960 01:25:30", "01-01-1960 1:05:36", "01-01-1960 02:25:59", "01-01-1960 1:20:30")
df <- as.data.frame(cbind(a,b))
df

hr <- hour(parse_date_time(b, "dmy HMS"))
minu <- minute(parse_date_time(b, "dmy HMS"))
sec<- second(parse_date_time(b, "dmy HMS"))

getDate <- as_date(parse_date_time(a, "dmy HMS"))
getTime <- paste(hr, minu, sec, sep = ":")

as_datetime(paste(getDate, getTime))
库(lubridate)

a使用基函数,我们可以做到:

a = as.POSIXct(a, '%d-%m-%Y %H:%M:%S', tz = "GMT")
b = as.POSIXct(b, '%d-%m-%Y %H:%M:%S', tz = "GMT")
df <- data.frame(a,b)
df$merged = paste(strftime(df$a, '%d-%m-%Y', tz = "GMT"), strftime(df$b, '%H:%M:%S', tz = "GMT"))
df

# 
#            a                   b              merged
# 1 2007-01-01 1960-01-01 01:25:30 01-01-2007 01:25:30
# 2 2007-02-01 1960-01-01 01:05:36 01-02-2007 01:05:36
# 3 2007-05-03 1960-01-01 02:25:59 03-05-2007 02:25:59
# 4 2007-08-31 1960-01-01 01:20:30 31-08-2007 01:20:30
a=as.POSIXct(a,'%d-%m-%Y%H:%m:%S',tz=“GMT”)
b=as.POSIXct(b,'%d-%m-%Y%H:%m:%S',tz=“GMT”)

df使用基函数,我们可以做到:

a = as.POSIXct(a, '%d-%m-%Y %H:%M:%S', tz = "GMT")
b = as.POSIXct(b, '%d-%m-%Y %H:%M:%S', tz = "GMT")
df <- data.frame(a,b)
df$merged = paste(strftime(df$a, '%d-%m-%Y', tz = "GMT"), strftime(df$b, '%H:%M:%S', tz = "GMT"))
df

# 
#            a                   b              merged
# 1 2007-01-01 1960-01-01 01:25:30 01-01-2007 01:25:30
# 2 2007-02-01 1960-01-01 01:05:36 01-02-2007 01:05:36
# 3 2007-05-03 1960-01-01 02:25:59 03-05-2007 02:25:59
# 4 2007-08-31 1960-01-01 01:20:30 31-08-2007 01:20:30
a=as.POSIXct(a,'%d-%m-%Y%H:%m:%S',tz=“GMT”)
b=as.POSIXct(b,'%d-%m-%Y%H:%m:%S',tz=“GMT”)

df使用
regex
将正确的部分组合在一起(仅假设中间有空格):


使用
regex
将正确的部分组合在一起(仅假设中间有空格):