将每个数据帧行按R中的向量划分 [英] Divide each data frame row by vector in R
问题描述
我正在尝试将数据帧中的每个数字分成16列,每列有一个特定的数字。数字被存储为与较大数据帧列1-16中的样本相对应的1-16的数据帧。每列有一个数字,我需要在较大的电子表格中除以每个数字,并将输出打印到最终的电子表格。
这是我的例子,从m开始。要分割的电子表格。
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
将电子表格分割为
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
而且预期的结果不一定像我在这里一样圆滑。
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
数据帧mx2 = mx / sf,其中mx是大数据集,sf是除以数字的数据帧。这似乎将所有内容除以sf数据集中的第一个数字。
划分的数字由estimateSizeFactors生成,这是DESeq包的一部分,如果有帮助的话。 p>
任何帮助都会很棒。谢谢!
sweep
对于这些操作很有用。例如,一些虚拟数据,我们将矩阵 mat
的相应列中的每个元素除以向量 vec
:
mat< - 矩阵(1:25,ncol = 5)
vec< - seq 2,by = 2,length = 5)
sweep(mat,2,vec,`/`)
在使用中我们有:
> 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
>扫描(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
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!
sweep
is useful for these sorts of operations. 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
这篇关于将每个数据帧行按R中的向量划分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!