Hey Guys I want to divide(numerically) all the columns of a data frame by different numbers. Here is what I am doing but getting a weird error. The values in each column are not getting divided by the corresponding value in the denominator vector instead by the alternative values. I am sure I am messing up something. Appreciate your help -Abhi head(counts) WT_CON WT_RB MU_CON MU_RB row1 839 180 477 187 head(counts/c(2,3,4,5)) WT_CON WT_RB MU_CON MU_RB row1 419.50000 45.0000 238.5000 46.7500
On Feb 6, 2012, at 2:22 PM, Abhishek Pratap wrote:> Hey Guys > > I want to divide(numerically) all the columns of a data frame by > different numbers. Here is what I am doing but getting a weird error. > The values in each column are not getting divided by the corresponding > value in the denominator vector instead by the alternative values. I > am sure I am messing up something. > > Appreciate your help > -Abhi > > head(counts) > WT_CON WT_RB MU_CON MU_RB > row1 839 180 477 187 > > > head(counts/c(2,3,4,5)) > WT_CON WT_RB MU_CON MU_RB > row1 419.50000 45.0000 238.5000 46.7500The argument recycling is going to be done on a column basis. You apaear to expect it to be done on a row basis. For that you need apply Probably: t( apply(counts, 1, "/", c(2,3,4,5)) ) But untested in absence of example. --- David Winsemius, MD West Hartford, CT
On 06-02-2012, at 20:22, Abhishek Pratap wrote:> Hey Guys > > I want to divide(numerically) all the columns of a data frame by > different numbers. Here is what I am doing but getting a weird error.I don't think you are getting an error. You are getting an unexpected result (you think).> The values in each column are not getting divided by the corresponding > value in the denominator vector instead by the alternative values. I > am sure I am messing up something. > > Appreciate your help > -Abhi > > head(counts) > WT_CON WT_RB MU_CON MU_RB > row1 839 180 477 187 > > > head(counts/c(2,3,4,5)) > WT_CON WT_RB MU_CON MU_RB > row1 419.50000 45.0000 238.5000 46.7500I tried this df <- data.frame(a=runif(10),b=runif(10),c=runif(10)) scal <- c(2,3,4)> dfa b c 1 0.47661685 0.73457617 0.90045279 2 0.06916502 0.76374600 0.07630196 3 0.17029174 0.29450289 0.07416969 4 0.03126839 0.09864740 0.08230353 5 0.23713816 0.06342224 0.91241698 6 0.21970595 0.86890690 0.47316101 7 0.46380324 0.26142304 0.87823277 8 0.81256517 0.76097474 0.98956553 9 0.74425369 0.29228545 0.27496707 10 0.65425285 0.40166967 0.12231213> df/scala b c 1 0.23830843 0.24485872 0.22511320 2 0.02305501 0.19093650 0.03815098 3 0.04257293 0.14725144 0.02472323 4 0.01563419 0.03288247 0.02057588 5 0.07904605 0.01585556 0.45620849 6 0.05492649 0.43445345 0.15772034 7 0.23190162 0.08714101 0.21955819 8 0.27085506 0.19024369 0.49478276 9 0.18606342 0.14614273 0.09165569 10 0.32712642 0.13388989 0.03057803 Checking:> df[4,]/scala b c 4 0.01563419 0.03288247 0.02057588 This is what you want guessing from your description. You haven't given us an example with desired output. Berend