[R] dividing values of each column in a dataframe

David Winsemius dwinsemius at comcast.net
Mon Feb 6 20:31:55 CET 2012


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.7500

The 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



More information about the R-help mailing list