0

我有两个数据集,AAPL并且AMZN,我希望两个合并,但发现很难这样做,因为merge cbind没有按照我的意愿去做。我认为问题在于将数据集识别为 data.frames 但不确定。

数据如下所示:

      Date Time   Open   High    Low  Close  Volume
1 12/14/12 9:30 514.75 515.10 512.72 512.86 2504264
2 12/14/12 9:31 512.80 513.00 510.00 510.17  574498
3 12/14/12 9:32 510.04 511.70 509.11 511.26  673126
4 12/14/12 9:33 511.26 511.54 508.82 509.25  477914
5 12/14/12 9:34 509.03 510.65 508.50 510.54  432689

期望的结果:

    Date Time   Open   High    Low  Close Volume
12/14/12 9:30 250.11 250.64 250.07 250.37  38249
12/14/12 9:31 250.60 250.60 250.16 250.51   6954
12/14/12 9:32 250.47 250.72 250.43 250.72   3843
12/14/12 9:33 250.69 250.70 250.44 250.50   3990
12/14/12 9:34 250.46 250.64 250.21 250.31   4490

    Date Time   Open   High    Low  Close Volume
12/14/12 9:31 512.80 513.00 510.00 510.17 574498
12/14/12 9:32 510.04 511.70 509.11 511.26 673126
12/14/12 9:33 511.26 511.54 508.82 509.25 477914
12/14/12 9:34 509.03 510.65 508.50 510.54 432689

本质上,我想并排Date合并这两个数据集(我不能在这里做)。我尝试将每个数据集转换为但不确定它是否正确:Time xts

AAPL <- read.csv("aapl1.csv",header=TRUE)
AMZN <- read.csv("amzn1.csv",header=TRUE)
aapl <- xts(AAPL[,c(3:7)], AAPL$DATETIME <-as.POSIXct(paste(AAPL$Date,AAPL$Time), format=""%m/%d/%Y %H:%M"))
amzn <- xts(AMZN[,c(3:7)], AMZN$DATETIME <-as.POSIXct(paste(AMZN$Date,AMZN$Time), format=""%m/%d/%Y %H:%M"))

cbind然后,当我使用,merge或什至 时,它无法合并join

4

3 回答 3

2

如果您的xts对象按日期时间索引(应该如此),只需将这两个集合传递给合并。在这里,我将自己合并一个集合,因为您的问题缺少示例数据:

data(sample_matrix)
sample.xts <- as.xts(head(sample_matrix), descr='my new xts object') # From ?xts

 merge(sample.xts, sample.xts)
##                Open     High      Low    Close   Open.1   High.1    Low.1  Close.1
## 2007-01-02 50.03978 50.11778 49.95041 50.11778 50.03978 50.11778 49.95041 50.11778
## 2007-01-03 50.23050 50.42188 50.23050 50.39767 50.23050 50.42188 50.23050 50.39767
## 2007-01-04 50.42096 50.42096 50.26414 50.33236 50.42096 50.42096 50.26414 50.33236
## 2007-01-05 50.37347 50.37347 50.22103 50.33459 50.37347 50.37347 50.22103 50.33459
## 2007-01-06 50.24433 50.24433 50.11121 50.18112 50.24433 50.24433 50.11121 50.18112
## 2007-01-07 50.13211 50.21561 49.99185 49.99185 50.13211 50.21561 49.99185 49.99185

这是有效的,因为merge需要merge.xts这些数据。

这是您的示例数据的合并,不使用xts. 首先,让我们将它们读入解释器:

AAPL <- read.table(header=T, text='Date Time Open High Low Close Volume
12/14/12 9:30 250.11 250.64 250.07 250.37 38249
12/14/12 9:31 250.60 250.60 250.16 250.51 6954
12/14/12 9:32 250.47 250.72 250.43 250.72 3843
12/14/12 9:33 250.69 250.70 250.44 250.50 3990
12/14/12 9:34 250.46 250.64 250.21 250.31 4490')

AMZN <- read.table(header=T, text='Date Time Open High Low Close Volume
12/14/12 9:31 512.80 513.00 510.00 510.17 574498
12/14/12 9:32 510.04 511.70 509.11 511.26 673126
12/14/12 9:33 511.26 511.54 508.82 509.25 477914
12/14/12 9:34 509.03 510.65 508.50 510.54 432689')

这些现在是类的对象,data.frame可以在DateTime列上合并:

merge(AAPL, AMZN, by=c('Date', 'Time'), all=T, suffixes = c('.AAPL', '.AMZN'))
##       Date Time Open.AAPL High.AAPL Low.AAPL Close.AAPL Volume.AAPL Open.AMZN High.AMZN Low.AMZN Close.AMZN Volume.AMZN
## 1 12/14/12 9:30    250.11    250.64   250.07     250.37       38249        NA        NA       NA         NA          NA
## 2 12/14/12 9:31    250.60    250.60   250.16     250.51        6954    512.80    513.00   510.00     510.17      574498
## 3 12/14/12 9:32    250.47    250.72   250.43     250.72        3843    510.04    511.70   509.11     511.26      673126
## 4 12/14/12 9:33    250.69    250.70   250.44     250.50        3990    511.26    511.54   508.82     509.25      477914
## 5 12/14/12 9:34    250.46    250.64   250.21     250.31        4490    509.03    510.65   508.50     510.54      432689
于 2014-01-17T03:27:59.127 回答
1

merge一旦您修复了代码中的一些问题,转换为 xts 并使用就可以了。

AAPL <- read.csv("aapl1.csv",header=TRUE)
AMZN <- read.csv("amzn1.csv",header=TRUE)
# your code is easier to understand if you create these columns outside of the
# xts constructor. Note that your `format` was incorrect. You need %y
# (2-digit year), not %Y (4-digit year). You also had unmatched quotes.
AAPL$DATETIME <- as.POSIXct(paste(AAPL$Date,AAPL$Time), format="%m/%d/%y %H:%M")
AMZN$DATETIME <- as.POSIXct(paste(AMZN$Date,AMZN$Time), format="%m/%d/%y %H:%M")
# create xts objects and merge
aapl <- xts(AAPL[,c(3:7)], AAPL$DATETIME)
amzn <- xts(AMZN[,c(3:7)], AMZN$DATETIME)
aapl.amzn <- merge(aapl,amzn)
于 2014-01-17T15:22:45.957 回答
1

第二种选择join()来自plyr包装。它有一些优势merge(),但也提供了更少的选择。对于非常大的数据集是值得推荐的,因为它比merge().

require(plyr)
join(AAPL, AMZN, by = c("Date", "Time"))
于 2014-01-17T09:14:12.450 回答