Try to search your question here, if you can't find : Ask Any Question Now ?

# Efficient way to build complex dataframe row by row in R

HomeCategory: stackoverflowEfficient way to build complex dataframe row by row in R

ðŸ™‚ Hi all

yes, I know there are already some tips and tricks out there how to efficiently build a dataframe row by row, but my for loop is still way to slow. Maybe it’s obvious for one of you how to speed it up/what’s the reason why it’s so slow.

As you can see I already converted it to a list, following the instructions of: Creating an R dataframe row-by-row but this does not make it much faster than rbind is.

``````index = 1

for (i in 1:nrow(predictionDf)) {

startDate = predictionDf\$ApropYMD[i]
amountPerMonth = (predictionDf\$PredictionExp[i]/12)
amountPerMonthMax = (predictionDf\$PredictionMax[i]/12)

print(i)

for (j in 1:12) {

plotDf[index, ] = list(ApropYMD=startDate, AmountExp = amountPerMonth, AmountMax = amountPerMonthMax)
month(startDate) <- month(startDate) + 1
index = index + 1
}

}
``````

I also tried this, which is a little faster

``````plotDf = data.frame("ApropYMD" =  c(seq(firstDayNextMonth, highestDate, by="months")))
plotDf\$AmountExp = 0
plotDf\$AmountMax = 0

for (i in 1:nrow(tmpPredictionDf)) {

startDate = tmpPredictionDf\$ApropYMD[i]
amountPerMonth = (tmpPredictionDf\$PredictionExp[i]/12)
amountPerMonthMax = (tmpPredictionDf\$PredictionMax[i]/12)

print(i)

for (j in 1:12) {

plotDf\$AmountExp[which(plotDf\$ApropYMD == startDate)] = plotDf\$AmountExp[which(plotDf\$ApropYMD == startDate)] + amountPerMonth
plotDf\$AmountMax[which(plotDf\$ApropYMD == startDate)] = plotDf\$AmountMax[which(plotDf\$ApropYMD == startDate)] + amountPerMonthMax
month(startDate) <- month(startDate) + 1

}
``````

I want to spread the Amount of one date over the next 12 months for each of the ~5.500 rows -> 5.500*12 = 66.000 row inserts

``````Input
ApropYMD    AmountExp   AmountMax
2019-01-01  6000    12000

Output
ApropYMD    AmountExp   AmountMax
2019-01-01  500 1000
2019-01-02  500 1000
2019-01-03  500 1000
2019-01-04  500 1000
2019-01-05  500 1000
2019-01-06  500 1000
2019-01-07  500 1000
2019-01-08  500 1000
2019-01-09  500 1000
2019-01-10  500 1000
2019-01-11  500 1000
2019-01-12  500 1000
``````