Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide each data frame row by vector in R

Tags:

I'm trying to divide each number within a data frame with 16 columns by a specific number for each column. The numbers are stored as a data frame with 1-16 corresponding to the samples in the larger data frames columns 1-16. There is a single number per column that I need to divide by each number in the larger spreadsheet and print the output to a final spreadsheet.

Here's and example of what I'm starting with. The spreadsheet to be divided.

            X131.478.1 X131.478.2 X131.NSC.1 X131.NSC.2 X166.478.1 X166.478.2 1/2-SBSRNA4          4          2          2          6          7          6 A1BG                93         73         88         86         58         65 A1BG-AS1           123        103         96        128         46         57 

The numbers to divide the spreadsheet by

X131.478.1 1.0660880 X131.478.2 0.9104053 X131.NSC.1 0.8642545 X131.NSC.2 0.9611866 X166.478.1 0.9711406 X166.478.2 1.0560121 

And the expected results, not necessarily rounded as I did here.

    X131.478.1 X131.478.2 X131.NSC.1 X131.NSC.2 X166.478.1 X166.478.2 1/2-SBSRNA4          3.75          2.19          2.31          6.24          7.20         5.68 A1BG                87.23         80.17         101.82         89.47         59.72         61.55 A1BG-AS1           115.37        113.13         111.07        133.16         47.36         53.97 

I tried simply dividing the data frames mx2 = mx/sf with mx being the large data set and sf being the data frame of numbers to divide by. That seemed to divide everything by the first number in the sf data set.

The numbers for division were generated by estimateSizeFactors, part of the DESeq package if that helps.

Any help would be great. Thanks!

like image 428
Ramma Avatar asked Dec 12 '12 00:12

Ramma


People also ask

How do I divide all cells by a number in R?

Data Visualization using R Programming To divide each column by a particular column, we can use division sign (/). For example, if we have a data frame called df that contains three columns say x, y, and z then we can divide all the columns by column z using the command df/df[,3].

How do you divide a matrix in R?

R Matrix Division To divide elements of a matrix with the corresponding elements of other matrix, use division (/) operator. The multiplication happens only between the (i,j) of first matrix and (i,j) of second matrix.

How do I break data into a group in R?

Split() is a built-in R function that divides a vector or data frame into groups according to the function's parameters. It takes a vector or data frame as an argument and divides the information into groups. The syntax for this function is as follows: split(x, f, drop = FALSE, ...)


2 Answers

sweep is useful for these sorts of operations, but it requires a matrix as input. As such, convert your data frame to a matrix, do the operation and then convert back. For example, some dummy data where we divide each element in respective columns of matrix mat by the corresponding value in the vector vec:

mat <- matrix(1:25, ncol = 5) vec <- seq(2, by = 2, length = 5)  sweep(mat, 2, vec, `/`) 

In use we have:

> mat      [,1] [,2] [,3] [,4] [,5] [1,]    1    6   11   16   21 [2,]    2    7   12   17   22 [3,]    3    8   13   18   23 [4,]    4    9   14   19   24 [5,]    5   10   15   20   25 > vec [1]  2  4  6  8 10 > sweep(mat, 2, vec, `/`)      [,1] [,2]     [,3]  [,4] [,5] [1,]  0.5 1.50 1.833333 2.000  2.1 [2,]  1.0 1.75 2.000000 2.125  2.2 [3,]  1.5 2.00 2.166667 2.250  2.3 [4,]  2.0 2.25 2.333333 2.375  2.4 [5,]  2.5 2.50 2.500000 2.500  2.5 > mat[,1] / vec[1] [1] 0.5 1.0 1.5 2.0 2.5 

To convert from a data frame use as.matrix(df) or data.matrix(df), and as.data.frame(mat) for the reverse.

like image 97
Gavin Simpson Avatar answered Oct 01 '22 21:10

Gavin Simpson


Suppose we have a dataframe, df:

> df   a b   c 1 1 3 100 2 2 4 110 

And we want to divide through each row by the same vector, vec:

> vec <- df[1,] > vec   a b   c 1 1 3 100 

Then we can use mapply as follows:

> mapply('/', df, vec)      a        b   c [1,] 1 1.000000 1.0 [2,] 2 1.333333 1.1 
like image 29
Ben Wynne-Morris Avatar answered Oct 01 '22 21:10

Ben Wynne-Morris