简体   繁体   English

将数据帧与R中的向量合并

[英]merge data frame with vector in R

Based on a t-test on gene expression data from microarray (with the EMA package) and subsequent annotation I generated a data frame that looks like this: 基于对来自微阵列(带有EMA软件包)的基因表达数据的t检验和随后的注释,我生成了一个数据框,如下所示:

>

 head(rt.annot)
      affy_hg_u133_plus_2     probeID      Stat    RawpValue    AdjpValue entrezgene hgnc_symbol ensembl_gene_id
14744           204103_at   204103_at 11.754856 1.718688e-20 9.396926e-16       6351        CCL4 ENSG00000275302
721            1553177_at  1553177_at 10.358405 1.810027e-17 4.948161e-13     117157      SH2D1B ENSG00000198574
16279         205495_s_at 205495_s_at  9.909715 1.721748e-16 3.137886e-12      10578        GNLY ENSG00000115523
21763           210163_at   210163_at  9.496225 1.374429e-15 1.623589e-11         NA        <NA>            <NA>
44641           230464_at   230464_at  9.480850 1.484763e-15 1.623589e-11      53637       S1PR5 ENSG00000180739
18998           207840_at   207840_at  9.383745 2.417818e-15 1.652428e-11      11126       CD160 ENSG00000117281

with 60376 rows and 8 columns. 60376行和8列。

I also measured the fold change of gene expression between the 2 groups, this generated a vector: 我还测量了两组之间基因表达的倍数变化,这产生了一个载体:

> head(fcOUT)
1007_s_at   1053_at    117_at    121_at 1255_g_at   1294_at 
0.9436815 1.0098279 1.0230719 0.9826041 0.9917645 1.0906764 

How do I merge the data frame (rt.annot) and vector (fcOUT) (so that the vector is aligned as a column to the matrix, based on the first column aafy_hg_u133_plus_2 (thus not as cbind function))? 如何合并数据帧(rt.annot)和向量(fcOUT)(以便根据第一列aafy_hg_u133_plus_2(因此不作为cbind函数)将向量对齐为矩阵的一列)? I could not find the answer elsewhere. 我在其他地方找不到答案。

Thank you! 谢谢!

Extract the names from your vector into a data frame, and then join that data frame onto your main data. 从向量中提取名称到数据框,然后将该数据框连接到主数据上。 Simple example to do that below: 下面是执行此操作的简单示例:

v1 <- as.vector(c(1,2,3))
names(v1) <- c('a', 'b', 'c')

df <- data.frame('affy_hg_u133_plus_2' = names(v1), 'values' = v1)

Then you'd be able to merge df onto your main data frame by affy_hg_u133_plus_2. 然后,您可以通过affy_hg_u133_plus_2将df合并到主数据帧中。

Did you try merge ? 您是否尝试过merge

a <- c("c1", "c2", "c3")
x <- 1:3
y <- runif(3)

foo <- data.frame(a = a, x = x)
bar <- data.frame(a = a, y = y)
merge(foo, bar, by = "a")

Also, please read this , and make your examples minimal and reproducible. 另外,请阅读此内容 ,并使示例最少且可重复。

Either you build a dataframe from your vector and join them: 您可以从矢量构建数据框并加入它们:

d.fcOUT = data.frame(aafy_hg_u133_plus_2 = names(fcOUT),
                                 fcOUT = fcOUT, stringsAsFactors=F)
library(dplyr)
left_join(rt.annot, d.fcOUT)

Or you use match() function: 或者您使用match()函数:

rt.annot$fcOUT = fcOUT[match(rt.annot$aafy_hg_u133_plus_2, names(fcOUT))]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM