-3

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!

Rachel
  • 3
  • 1
  • Welcome to SO! Please read [this post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to ask a good question regarding R and edit the post accordingly. Particularly, please include the output of ``dput(head(data))`` in your original post rather than describing your dataset, since it will make it much easier for people to help you. Thanks. – user438383 Jan 12 '21 at 09:38

1 Answers1

-1

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.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213