我有代码工作。但速度很慢,我希望能够加快速度,这样我就可以扩展到几十万个观察值的数据集。查找匹配并追加到data.frame的更快方法?
我有两个数据帧,其中一个我使用data.table包转换为data.table包以便快速查找和连接。当3个字段与第二个数据集中的记录相匹配时,我想记录一个数据集中的记录。
Original.df(数据框)和LookHereForMatches.dt(带有a1,a2,a3上的键的data.table)。 Original.df将有10万到30万的观察值,LookHereForMatches.dt可能会是2倍。
我循环遍历Original.df中的每个观察值,并在LookHereForMatches.dt中查找与某些条件匹配的观察值。我需要从LookHereForMatches.dt中的几个字段和来自Original.df的几个字段。我使用subset()来获得我想要的列。
也许有人可以告诉我,我的代码的哪一部分是最差/最慢的。我必须相信它是rbind(cbind())的一部分。似乎并不是这样做的正确方法。
matched_data.df <- data.frame()
for(i in 1:nrow(Original.df)){
a1 <- Original.df$col1
a2 <- Original.df$col2
a3 <- Original.df$col3
# Use data.table library "join" functionality to get matches (will find at least 1 and up to 4 matches, usually only 1 or 2)
match.df <- data.frame(LookHereForMatches.dt[J(a1, a2, a3)], stringsAsFactors=FALSE)
# combine matches with original data and add to data.frame to create big list of data with matches
matched_data.df <- rbind(cbind(match.df, Original.df[i,], stringsAsFactors=FALSE), matched_data.df)
}
UPDATE
这里是大致的数据是什么样子。 (显然是R和StackExchange上的新手
我会弄清楚如何使表更漂亮并回来解决这个问题。
感谢@joran修复我的表。)表是非常基本的东西。我只想查找第一个表中的每一行,并将其与第一个表a1,a2和a3中所有适当的行进行匹配。在该示例中,来自Original.df的第一行应该与返回3行的LookHereForMatches.dt表中的行1,行2和行3配对。
Original.df <- read.table(textConnection('
a1 a2 a3 text.field numeric.field
123 abc 2011-12-01 "some text" 1.0
124 abc 2011-11-12 "some other text" 0.1
125 bcd 2011-12-01 "more text" 1.2
'), header=TRUE)
LookHereForMatches.df <- read.table(textConnection('
a1 a2 a3 text.field numeric.field Status_Ind
123 abc 2011-12-01 "some text" 10.5 0
123 abc 2011-12-01 "different text" 0.1 1
123 abc 2011-12-01 "more text" 0.1 1
125 bcd 2011-12-01 "other text" 4.3 0
125 bcd 2011-12-01 "text" 2.2 0
'), header=TRUE)
LookHereForMatches.dt <- data.table(LookHereForMatches.df, key=c("a1","a2","a3"))
因为我不知道你的数据是什么样子,原谅我,如果这没有帮助......如果你能提供数据的一个小样本,您将得到更好的答案。但是,你不能使用像条件匹配的东西吗? 'Origional.df [Origion.df $ a1%in%LookHereForMatchers.dt $ a1&Origional.df $ a2%in%LookHereForMatches.dt $ a2,]'。 'for循环'很慢,但'rbind(cbind(...))'慢得多。理想情况下,您可以在分配之前分配全尺寸的'matched_data.df'。如果你不能,使用像我上面写的东西应该可以帮助一些...... – Justin 2012-04-23 22:13:49
我不明白(也许是因为你没有提供一个可重复的例子?)为什么你不能简单地在data.table之间做一个连接。 – joran 2012-04-23 22:33:41
更新后添加一些示例数据。我会查看%in%。至于我无法在data.tables之间进行连接的原因...我是R新手。我也会考虑加入。 – user791770 2012-04-23 22:38:48