1

I have a large data.frame made of characters and numeric variables. I need to divide, let's say, column 6 to columns 4 5 6, and keeping 1 2 3 as they are.

Francesco
  • 59
  • 2
  • 9
  • Could you please give us some example data and perhaps the desired result after your proposed operation? Right now your question is a bit ambiguous. – Alex Witsil Sep 18 '16 at 22:51
  • 1
    Please have a wee read of http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example, which will help you create an example. I suspect you just need `yourdataframe[4:6] <- yourdataframe[4:6] / yourdataframe[,6]` – user20650 Sep 18 '16 at 23:02
  • Thank You for your answers. I have a data.frame made of 23 columns that I obtained using the decompr package. I have to divide element by element the columns from 4 to 22 with the column 20. I found difficulty since columns 1:3 are factors, while other columns are numerics. The answer provided by mr @AlexMiller was what I needed. – Francesco Sep 19 '16 at 14:45

1 Answers1

2

Your question is a bit vague (for instance, do you want element wise division?), but is this what you're looking for?

 ## set up some test data
 data.org=data.frame(matrix(1:100,ncol=10))

 ## make a copy of the org. data
 data=data.org

 ## perform your element by element division
 data[,4] = data.org[,4]/data.org[,6]
 data[,5] = data.org[,5]/data.org[,6]
 data[,6] = data.org[,6]/data.org[,6]

 ## Or the entire operation can be done with one line by
 data[,4:6] = data.org[,4:6] / data.org[,6]
Alex Witsil
  • 855
  • 8
  • 22
  • I think one of the matrices here would have to be converted from numeric, using `as.numeric()`, if I understand the OP correctly. That would be something like `as.numeric(data[,4:6]) / data.org[,6]` – Bastiaan Quast Sep 19 '16 at 14:35