【发布时间】:2017-02-03 02:22:13
【问题描述】:
我在使用 R 中的合并函数处理两个数据帧时遇到了一点问题。我有两个大型数据框,它们的列和数据彼此相同,这就是我用来合并它们的方法。
例如,数据框 1 具有列“Instrument”、“RecordDate”、“HourMinuteSecond”、“MilliSecond”、...(以及其他几个附加列),而数据框 2 具有列“Instrument”、“RecordDate” ", "HourMinuteSecond", "MilliSecond", .... (以及与数据框 2 相比的其他几个不同的列)。我现在使用合并功能如下:
DataFrame3 <- merge(DataFrame2, DataFrame1, by=c("Instrument", "RecordDate","HourMinuteSecond","MilliSecond"))
这给了我一个新的数据框。现在,我遇到的问题是数据帧之间的一些根本不匹配。比如我发现有一段数据没有正确匹配:
> DataFrame1[120486,]
Instrument RecordDate HourMinuteSecond MilliSecond
120486 DTE 6/4/2012 16:10:27 42
> DataFrame2[65,]
Instrument RecordDate HourMinuteSecond MilliSecond
65 DTE 6/4/2012 16:10:27 42
(请注意,还有其他列,但我省略了它们)。 我现在比较这些部分,就好像它们是向量一样。首先,我使用相同的函数来比较每个向量中的每个值,结果如下:
> identical(DataFrame1[120486,1] ,DataFrame2[65,1])
[1] FALSE
> identical(DataFrame1[120486,2] ,DataFrame2[65,2])
[1] TRUE
> identical(DataFrame1[120486,3] ,DataFrame2[65,3])
[1] FALSE
> identical(DataFrame1[120486,4] ,DataFrame2[65,4])
[1] TRUE
从相同的函数看来,“Instruments”和“HourMintuteSecond”列中的值似乎彼此不同。谁能告诉我可能导致此问题的原因是什么?提前致谢。
编辑:这是 dput 输出,希望这就是你们所指的:
> dput(droplevels(DataFrame2[65,1:4]))
structure(list(Instrument = structure(1L, .Label = "DTE", class = "factor"),
RecordDate = structure(1L, .Label = "6/4/2012", class = "factor"),
HourMinuteSecond = structure(1L, .Label = "16:10:27", class = "factor"),
MilliSecond = 42L), .Names = c("Instrument", "RecordDate",
"HourMinuteSecond", "MilliSecond"), row.names = 65L, class = "data.frame")
> dput(droplevels(DataFrame1[120486,1:4]))
structure(list(Instrument = structure(1L, .Label = "DTE", class = "factor"),
RecordDate = structure(1L, .Label = "6/4/2012", class = "factor"),
HourMinuteSecond = structure(1L, .Label = "16:10:27", class = "factor"),
MilliSecond = 42L), .Names = c("Instrument", "RecordDate",
"HourMinuteSecond", "MilliSecond"), row.names = 120486L, class = "data.frame")
这里是 str() 输出:
> str(DataFrame1)
'data.frame': 317495 obs. of 9 variables:
$ Instrument : Factor w/ 4 levels "CDD","DTE","ERA",..: 1 1 1 1 1 1 1 1 1 1 ...
$ RecordDate : Factor w/ 30 levels "5/18/2012","5/21/2012",..: 1 1 1 1 1 1 1 1 1 1 ...
$ HourMinuteSecond: Factor w/ 21763 levels "10:02:02","10:02:03",..: 14 14 14 17 19 22 24 25 25 25 ...
$ MilliSecond : int 26 57 158 70 73 8 926 448 457 458 ...
$ L1BidPrice : num 6.91 6.91 6.91 6.91 6.91 6.91 6.9 6.9 6.89 6.89 ...
$ L1BidVolume : int 520 504 504 504 504 508 20 4 20 20 ...
$ L1AskPrice : num 6.92 6.92 6.92 6.92 6.92 6.92 6.91 6.91 6.9 6.9 ...
$ L1AskVolume : int 3917 3917 3915 3932 3915 3915 3407 3407 13 30 ...
$ Midquote : num 6.92 6.92 6.92 6.92 6.92 ...
> str(DataFrame2)
'data.frame': 577 obs. of 15 variables:
$ Instrument : Factor w/ 2 levels "DTE","ERA": 1 1 1 1 1 1 1 1 1 1 ...
$ RecordDate : Factor w/ 30 levels "5/18/2012","5/21/2012",..: 1 1 1 1 1 1 2 2 2 2 ...
$ HourMinuteSecond : Factor w/ 317 levels "10:02:10","10:02:21",..: 301 301 301 301 301 301 2 98 129 130 ...
$ MilliSecond : int 45 45 45 45 45 45 485 6 92 300 ...
$ RecordType : Factor w/ 1 level "TRADE": 1 1 1 1 1 1 1 1 1 1 ...
$ Price : num 0.195 0.195 0.195 0.195 0.195 0.195 0.2 0.19 0.19 0.185 ...
$ Volume : int 2686 6350 6350 6350 1620 3064 1 13986 25000 23092 ...
$ UndisclosedVolume: Factor w/ 1 level "\\N": 1 1 1 1 1 1 1 1 1 1 ...
$ DollarValue : num 524 1238 1238 1238 316 ...
$ Qualifiers : Factor w/ 4 levels "\\N","AC","Bi",..: 2 2 2 2 2 2 4 4 3 4 ...
$ BidID : num 6.13e+18 6.13e+18 6.13e+18 6.13e+18 6.13e+18 ...
$ AskID : num 6.13e+18 6.13e+18 6.13e+18 6.13e+18 6.13e+18 ...
$ BidOrAsk : Factor w/ 1 level "\\N": 1 1 1 1 1 1 1 1 1 1 ...
$ BuyerBrokerID : int 229 229 229 229 229 229 236 129 229 112 ...
$ SellerBrokerID : int 297 210 210 210 110 157 229 229 299 229 ...
【问题讨论】:
-
请在此处使用
dput发布merge认为不相似的两条记录 -
也许你有因素,水平不一样?发布一些
dput(),例如dput(DataFrame2[65,1:4]),或者如果那太长了dput(droplevels(DataFrame2[65,1:4])),(另一行也类似)会让一切都清楚。 -
更有可能的是,如果您在两个框架上运行
str(),您会发现在第一列中,一个数据框架将它们作为一个因素,而另一个字符数据 (DTE)或者在不同的层次上,和第三列的时间,它们的形状是一样的,但它们可能是不同的时间格式,或者被列为不同层次的因素......使用str()来计算! -
嗨,我添加了 dput 输出,我使用了 droplevels,因为输出非常大,我似乎无法上传文件。另外,我现在试试 str() 函数:)
-
关于str(),不同级别对数据有何影响?不幸的是,我并不真正熟悉该功能或其含义。你知道我应该怎么做来解决它吗?