I have a data.frame
called d
on which I would like to make the following operation
d <- mutate(d,netto_0 = netto_0_pc * tot_pop)
d <- mutate(d,netto_1 = netto_1_pc * tot_pop)
d <- mutate(d,netto_2 = netto_2_pc * tot_pop)
d <- mutate(d,netto_3 = netto_3_pc * tot_pop)
d <- mutate(d,netto_4 = netto_4_pc * tot_pop)
d <- mutate(d,netto_5 = netto_5_pc * tot_pop)
d <- mutate(d,netto_6 = netto_6_pc * tot_pop)
d <- mutate(d,netto_7 = netto_7_pc * tot_pop)
d <- mutate(d,netto_8 = netto_8_pc * tot_pop)
d <- mutate(d,netto_9 = netto_9_pc * tot_pop)
I'd like to achieve this result in a more efficient way by performing a for-loop. Unfortunately variable names are not character strings, therefore the following code does not work.
for( k in 0:9 ){
newcol <- paste0( 'netto_', k )
oldcol <- paste0( 'netto_', k, '_pc' )
d <- mutate(d, newcol = oldcol * tot_pop)
}
Or also
for( k in 0:9 ){
newcol <- paste0( 'netto_', k )
oldcol <- paste0( 'netto_', k, '_pc' )
d$newcol < d$oldcol * d$tot_pop
}
How can I fix it?