R 中的 rbind 循环

rbind loop in R

我有 200 万行和 7 列的数据。还有数以千计的不同公司和指标。

数据样本是:

company_id                                                             indicator_id     X2011
1    1000045                 AccountsPayableAndAccruedLiabilitiesCurrentAndNoncurrent   6612429
2    1000045 AccumulatedDepreciationDepletionAndAmortizationPropertyPlantAndEquipment        NA
3    1000045                                                AdjustmentForAmortization        NA
4    1000045                                                                   Assets 257236034
5    1000045                                         AssetsHeldForSaleAtCarryingValue   1373001
6    1000045                      AssetsOfDisposalGroupIncludingDiscontinuedOperation        NA
      X2012     X2013     X2014     X2015
1   7405579   8924919   7841070   5839000
2   2111343   2242703   2236449   2462000
3 -11482251 -13490892 -13852305 -13811000
4 263835468 283429579 302528591 325309000
5   1203664   1696330        NA        NA
6        NA        NA   1746887   2148000

我想要的一些指标是:

    asset_indicators
[1] "DeferredIncomeTaxExpenseBenefit"                                         
[2] "CashAndCashEquivalentsAtCarryingValue"                                   
[3] "CashAndCashEquivalentsPeriodIncreaseDecrease"                            
[4] "NetCashProvidedByUsedInOperatingActivities"                              
[5] "NetCashProvidedByUsedInFinancingActivities"                              
[6] "NetCashProvidedByUsedInInvestingActivities"                              
[7] "PropertyPlantAndEquipmentNet"                                            

我正在尝试获取仅包含某些行的新数据框。目前,我有这个代码:

year <-rbind(statement[which(statement$indicator_id==asset_indicators[1]),],
         statement[which(statement$indicator_id==asset_indicators[2]),],
         statement[which(statement$indicator_id==asset_indicators[3]),],
         statement[which(statement$indicator_id==asset_indicators[4]),],
         statement[which(statement$indicator_id==asset_indicators[5]),],
         statement[which(statement$indicator_id==asset_indicators[6]),],
         statement[which(statement$indicator_id==asset_indicators[7]),] )

我想知道是否有更有效的方法?我尝试制作一个 for 循环,但 运行 花了几个小时,而这只需要几秒钟。如果我更改要使用的指标数量,它会自动更新,那就太好了。

有人知道怎么做吗?

尝试 %in%:

year <- statement[statement$indicator_id %in% asset_indicators,]