如何在R中创建DateTime变量

时间:2015-10-17 21:48:05

标签: r date datetime time posixct

我将一堆csv文件读入R并将它们绑定在一起,因此我有一个看起来像这样的巨型数据文件

此文件称为数据

Date         Time   pH
1976-01-26   6:00   4.00
1976-01-26   7:30   4.05
1976-01-26   13:30  4.50
1976-01-27   5:00   4.50

2000行

然后我尝试了

data$DateTime = as.POSIXct(data$Date, data$Time)

然而,这给了我这个结果

Date         Time   pH     DateTime
1976-01-26   6:00   4.00   1976-01-25 19:00:00
1976-01-26   7:30   4.05   1976-01-25 19:00:00
1976-01-26   13:30  4.50   1976-01-25 19:00:00
1976-01-27   5:00   4.50   1976-01-26 19:00:00

有关为何会发生这种情况的任何线索?

以下是我到达此处列出的数据框的确切代码

## BD files
setwd(DIR)
setwd("Acid Rain BD")
DIR.BD = getwd()
files = list.files(DIR.BD)

# read the files into a list of data.frames
data.list <- lapply(files, read.csv);
# concatenate into one big data.frame
data.cat.BD <- do.call(rbind, data.list);

#fix the dates
data.cat.BD$Date <- as.Date(data.cat.BD$Date, "%m/%d/%Y")

#Create the datetime variable (IMPORTANT ONLY DO THIS ONCE OR REDO)

data.cat.BD$DateTime = as.POSIXct(paste(data.cat.BD$Date, data.cat.BD$Time))

当我输入列出的代码

时会发生这种情况
> sample(unique(data.cat.BD$Date), min (length(data.cat.BD$Date),100))
  [1] "1984-08-20" "1979-09-06" "1984-07-17" "1982-01-23"
  [5] "1977-11-24" "1980-05-14" "1980-10-25" "1980-04-28"
  [9] "1981-09-15" "1979-03-06" "1976-03-16" "1985-09-08"
 [13] "1982-10-21" "1984-01-14" "1976-07-24" "1985-08-31"
 [17] "1976-02-19" "1985-11-29" "1984-03-14" "1983-11-25"
 [21] "1984-02-11" "1982-01-04" "1982-01-13" "1980-07-21"
 [25] "1976-08-27" "1982-03-04" "1983-09-30" "1977-03-19"
 [29] "1980-04-27" "1982-10-09" "1985-02-02" "1984-07-18"
 [33] "1977-10-15" "1984-03-06" "1983-05-01" "1983-03-11"
 [37] "1984-03-17" "1978-03-26" "1979-08-02" "1977-12-19"
 [41] "1984-12-22" "1982-03-17" "1977-10-17" "1985-10-02"
 [45] "1981-07-04" "1980-06-08" "1981-02-20" "1982-01-20"
 [49] "1979-02-07" "1985-07-21" "1977-01-14" "1982-09-03"
 [53] "1984-10-02" "1976-05-07" "1984-12-27" "1980-11-27"
 [57] "1985-01-31" "1978-01-26" "1979-08-10" "1985-10-04"
 [61] "1984-05-13" "1979-12-07" "1977-01-24" "1982-09-27"
 [65] "1980-03-21" "1982-10-26" "1984-06-30" "1981-04-28"
 [69] "1984-04-04" "1979-10-05" "1985-08-19" "1976-05-01"
 [73] "1976-02-05" "1984-02-03" "1982-04-17" "1977-04-25"
 [77] "1977-10-16" "1980-04-15" "1984-10-10" "1976-03-09"
 [81] "1979-05-03" "1980-11-18" "1976-02-17" "1983-03-20"
 [85] "1982-11-26" "1982-03-12" "1983-03-19" "1983-04-30"
 [89] "1985-12-07" "1981-02-02" "1981-02-08" "1982-02-09"
 [93] "1977-02-24" "1976-09-18" "1977-03-22" "1985-10-13"
 [97] "1981-06-13" "1985-01-15" "1976-09-20" "1977-12-15"

1 个答案:

答案 0 :(得分:2)

您可以使用paste来组合日期和时间:

df$DateTime <- as.POSIXct(paste(df$Date, df$Time))
df
#         Date  Time   pH            DateTime
# 1 1976-01-26  6:00 4.00 1976-01-26 06:00:00
# 2 1976-01-26  7:30 4.05 1976-01-26 07:30:00
# 3 1976-01-26 13:30 4.50 1976-01-26 13:30:00
# 4 1976-01-27  5:00 4.50 1976-01-27 05:00:00

可重复数据

df <- read.table(text="
Date         Time   pH
1976-01-26   6:00   4.00
1976-01-26   7:30   4.05
1976-01-26   13:30  4.50
1976-01-27   5:00   4.50", header=T)