0

I've looked on the internet but I haven found the answer that I'm looking for, but shure it's out there...

I've a data frame, and I want to divide (or any other operation) every cell of a row by a value that it's placed in the second column of my data frame. So first row from col3 to last col, divide each cell by the value of col2 of that certain row, and so on for every single row.

I have solved this by using a For loop, col2 (delta) it's now a vector, and col3 to end it's a data.frame (mu). The results are append to a new data frame by using rbind.

The question is; I'm pretty sure that this can be done by using the function apply, sapply or similar, but I have not gotten the results that I've been looking so far (not the good ones as I do with the loop for). ¿How can I do it without using a loop for?

Loop for I've been using so far.

In resume. I want to divide each mu by the delta value of it's own row.

for (i in 1:(dim(mu)[1])){
    RA_row <- mu[i,]/delta[i]
    RA <- rbind(RA, RA_row)
    }

transcript  delta   mu_5    mu_15   mu_25   mu_35   mu_45   mu_55   mu_65
1   YAL001C 0.066702720 2.201787e-01    1.175731e-01    2.372506e-01    0.139281317 0.081723456 1.835414e-01    1.678318e-01
2   YAL002W 0.106000180 3.685822e-01    1.326865e-01    2.887973e-01    0.158207858 0.193476082 1.867039e-01    1.776946e-01
3   YAL003W 0.022119345 2.271518e+00    2.390637e+00    1.651997e+00    3.802739732 2.733559839 2.772454e+00    3.571712e+00

Thanks

HeyHoLetsGo
  • 137
  • 1
  • 14
  • Please post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to completely understand what you are trying to accomplish. – digEmAll Nov 08 '14 at 17:49
  • Sorry for this, in the original data frame col1 is just the name for that particular row, so no operation should be done. I've saved col1 from the original data frame to paste it with RA data frame. In the mu data frame all cells must be divided. – HeyHoLetsGo Nov 08 '14 at 17:51
  • Again, please add an example of your input data.frame and your desired output. An example with 4-5 rows is enough to understand your problem completely. – digEmAll Nov 08 '14 at 17:53
  • Sorry again. I've pasted some rows. – HeyHoLetsGo Nov 08 '14 at 17:55
  • I think rather, `df[-(1:2),]/df[ ,2]` – IRTFM Nov 08 '14 at 17:55
  • Damn. I got the comma wrong. `df[-(1:2)]/df[ ,2]` is equivalent to `df[,-(1:2)]/df[ ,2]`. (My code below was tested.) – IRTFM Nov 08 '14 at 18:00
  • Thanks, I've tried `df[,-(1:2)]/df[ ,2]` and it gives me the same solution that I get with my loop. So that's a better solution that my loop, thanks!! – HeyHoLetsGo Nov 08 '14 at 18:04

1 Answers1

3

It appears as though you want just:

mu2 <- mu[-(1:2)]/mu[[2]]
#  same as mu[-(1:2), ]/mu[['delta']]

That should produce a new dataframe with the division by row. Somewhat more dangerous would be to do the division "in place".

 mu[-(1:2)] <- mu[-(1:2)]/mu[[2]]

> mu <- data.frame(a=1,b=1:10, c=rnorm(10), d=rnorm(10) )
> mu
   a  b           c           d
1  1  1 -1.91435943  0.45018710
2  1  2  1.17658331 -0.01855983
3  1  3 -1.66497244 -0.31806837
4  1  4 -0.46353040 -0.92936215
5  1  5 -1.11592011 -1.48746031
6  1  6 -0.75081900 -1.07519230
7  1  7  2.08716655  1.00002880
8  1  8  0.01739562 -0.62126669
9  1  9 -1.28630053 -1.38442685
10 1 10 -1.64060553  1.86929062
> (mu2 <- mu[-(1:2)]/mu[[2]])
              c            d
1  -1.914359426  0.450187101
2   0.588291656 -0.009279916
3  -0.554990812 -0.106022792
4  -0.115882600 -0.232340537
5  -0.223184021 -0.297492062
6  -0.125136500 -0.179198716
7   0.298166649  0.142861258
8   0.002174452 -0.077658337
9  -0.142922281 -0.153825205
10 -0.164060553  0.186929062
> (mu[-(1:2)] <- mu[-(1:2)]/mu[[2]] )
> mu
   a  b            c            d
1  1  1 -1.914359426  0.450187101
2  1  2  0.588291656 -0.009279916
3  1  3 -0.554990812 -0.106022792
4  1  4 -0.115882600 -0.232340537
5  1  5 -0.223184021 -0.297492062
6  1  6 -0.125136500 -0.179198716
7  1  7  0.298166649  0.142861258
8  1  8  0.002174452 -0.077658337
9  1  9 -0.142922281 -0.153825205
10 1 10 -0.164060553  0.186929062
IRTFM
  • 258,963
  • 21
  • 364
  • 487