I have a dataframe df:
Shares Price1 Price2 Price3
100 9 10 11
200 5 6 7
300 3 2 1
And I would like to loop over this dataframe and create three new columns equal to Shares x Price[i] where (i in 1:3). I tried the following code:
for (j in 1:3) {
df$paste0("MktCap",j,sep="")<-df$Shares*df$paste0("Price",j,sep="")
}
But I get the error:
Error: attempt to apply non-function
I've looked here but it's not quite what I want as I would like my new column names to iterate.