[英]Add column names of a dataframe or from an R object to another dataframe
I'm currently working with a huge count matrix issued of single cell sequencing ...我目前正在处理一个巨大的单细胞测序计数矩阵......
So, in order to analyze them with R and my 8 Gb of RAM, I had to split it in several sub-matrices.因此,为了使用 R 和我的 8 Gb RAM 分析它们,我不得不将其拆分为几个子矩阵。
I simply used split
in order to do that so I loose the heathers of the matrix.我只是使用
split
来做到这一点,所以我松开了矩阵的石南花。
So, I would like to add them back with R or find a better way to split them more efficiently.所以,我想用 R 重新添加它们,或者找到一种更好的方法来更有效地拆分它们。
My questions are:我的问题是:
1. If a have an object called heathers with all the column names stocked inside, is there a way to efficiently add this object to a dataframe? 1.如果有一个名为 heathers 的对象,其中包含所有列名,有没有办法有效地将此对象添加到数据框中? I tried
rbind
but it doesn't really solve the problem.我试过
rbind
但它并没有真正解决问题。
2. Is there a better way to cut those huge count matrices into multiple parts? 2.有没有更好的方法将那些巨大的计数矩阵切割成多个部分? (I can't do it through R because I don't have enough RAM, R crashes if I try to import the whole matrix)
(我不能通过 R 来完成,因为我没有足够的 RAM,如果我尝试导入整个矩阵,R 会崩溃)
You can access and mutate a data.frame
s column names with the names
function:您可以使用
names
函数访问和data.frame
的列名称:
df <- data.frame(foo = 1:5, bar = 6:10, opt = 11:15)
original_names <- names(df)
original_names
Returns:返回:
[1] "foo" "bar" "opt"
And to assign new names:并分配新名称:
names(df) <- c("new_col1", "new_col2", "new_col3")
Now:现在:
df
Returns:返回:
new_col1 new_col2 new_col3
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
And to 'undo' the renaming:并“撤消”重命名:
names(df) <- original_names
And df
has again its original names: df
再次拥有它的原始名称:
foo bar opt
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
You can add headers to a dataframe like this:您可以像这样向数据帧添加标题:
dataframe <- data.frame(c("a", "b","c"),
c("d", "e", "f"))
headers <- c("header_1" , "header_2")
names(dataframe) <- headers
dataframe
header_1 header_2
1 a d
2 b e
3 c f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.