我正在尝试将某些功能分组。下面的data.frame(分组)是我的“关键”(想想Excel vlookup):
Original Grouped
1 Features Constant
2 PhoneService Constant
3 PhoneServices Constant
4 Surcharges Constant
5 CallingPlans Constant
6 Taxes Constant
7 LDUsage Noise
8 RegionalUsage Noise
9 LocalUsage Noise
10 Late fees Noise
11 SpecialServices Noise
12 TFUsage Noise
13 VoipUsage Noise
14 CCUsage Noise
15 Credits Credits
16 OneTime OneTime
然后我引用我的数据库,它有一个列 (BillSection),它采用来自 grouped$Original 的特定值,我想根据 grouped$Grouped 对其进行分组。我正在使用 sapply 函数来执行此操作。然后我将结果输出绑定到我的原始data.frame。
grouper<-as.character(sapply(as.character(bill.data$BillSection[1:100]), # for the first 100 records of the data.frame bill.data
function(x)grouped[grouped$Original==x,2])) # take the second column, i.e. Grouped, for the corresponding "TRUE" value in Original
cbind(bill.data[1:100,],as.data.frame(grouper))
上面的代码按预期工作,但是当我将它应用到超过 10,000,000 条唯一记录的整个数据库时速度很慢。这种方法有替代方法吗?我知道我可以使用 plyr,但它甚至比 sapply 还要慢(我认为)。我试图用 data.table 解决这个问题,但没有运气。任何的意见都将会有帮助。我愿意用 Python 编写这个代码,我是新手,但听说比 R 快得多,因为我经常处理大型数据集。我想知道 R 是否可以足够快地做到这一点以便有用。
谢谢!