My dataset has 3000 observations (rows) and 24 variables (columns). First 12 (column 1 to 12) variables are price value from January to December in a year. The remaining 12 variables (column 13 to 24) are rent value from January to December in the same year. I would like to caculate the price/rent for each month and add the 12 new ratios columns to the end of my origianl dataset. How can I do this in R?
Thank you in advance!
You could do :
df[paste0('ratio', 1:12)] <- df[1:12]/df[13:24]
This will create 12 new columns called ratio1, ratio2 ... ratio12. where ratio1 is column1/column13 ratio2 is column2/column14 and so on.
ratio1
ratio2
ratio12
2.1m questions
2.1m answers
63 comments
56.7k users