简体   繁体   English

将数据框的列名或从 R 对象添加到另一个数据框

[英]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
  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?如果有一个名为 heathers 的对象,其中包含所有列名,有没有办法有效地将此对象添加到数据框中? I tried rbind but it doesn't really solve the problem.我试过 rbind 但它并没有真正解决问题。

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

  1. You could use bash for such tasks.您可以将 bash 用于此类任务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 R 中的另一个数据框列完成列名 - Complete column names with another dataframe column in R 如何通过在R中调用列名来合并来自另一个数据帧的数据 - How to combine data from another dataframe by calling column names in r R - 在另一个数据帧的行中插入数据帧中的值,列名作为 ID - R - Insert values from dataframe in row of another dataframe with column names as ID 根据R中另一个df2的信息添加列到dataframe - Add column to dataframe based on information from another df2 in R 如何通过根据另一个数据帧的行名的顺序映射一个数据帧的列名来对 R 中的数据帧进行排序? - How to sort a dataframe in R by mapping column names of one dataframe based on the order of row names of another dataframe? 使用来自另一个 dataframe 的值作为 R 中的变量名来改变 dataframe - mutating a dataframe using values from another dataframe as variable names in R R中数字数据帧的有序列名称数据帧的更快方法 - faster method for ordered column names dataframe from numeric dataframe in R R:通过计算来自另一个数据帧的 CSV 列中字符串的出现,将计数出现列添加到数据帧 - R: add a count occurrence column to dataframe by counting the occurrence of a string in a CSV column from another dataframe R 添加列以匹配另一个数据框 - R add column to match another dataframe 使用另一个数据帧的字符串中的列名动态聚合数据帧 - Aggregating a dataframe dynamically with column names in strings from another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM