简体   繁体   中英

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:

>

 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.

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))? 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.

Did you try 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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