0

我在 R 中很新,我想对我在 R 中的数据做一些事情。有人可以帮助我如何在 R 中实现它吗?

我有如下数据矩阵(mydata1),我想从第二个数据库中为其添加第二列。

我的第一个数据矩阵是这样的:

> mydata1[1:4,1:3]

 Gene ID              lung.cancer lung.cancer.1 lung.cancer.2
hsa-miR-616*      3.653241       1.00000      1.838179
hsa-miR-1296     2.688751      36.12798     43.823880
hsa-miR-338-5p   29.893947      2.21830     48.048856
hsa-miR-452*    5.693279    1015.58508   35.165157
> 

我的第二个数据库是这样的:

> Database 

      ENS ID           Gene ID

    ENSG00000221263   hsa-mir-548p
    ENSG00000207941   hsa-miR-616
    ENSG00000207800   hsa-mir-504
    ENSG00000222831   hsa-mir-1537
    ENSG00000207582   hsa-mir-30b
    ENSG00000199153   hsa-miR-338-5p
    ENSG00000215998   hsa-mir-935
    ENSG00000207804   hsa-mir-599

我想在调用Gene ID我的第一个数据ENS ID矩阵(mydata1mydata1DatabaseENS IDmydata1

预期的输出如下所示:

  Gene ID            ENS ID            lung.cancer lung.cancer.1 lung.cancer.2
    hsa-miR-616*     ENSG00000207941   5.653241       1.00000      1.838179
    hsa-miR-1296                       7.688751      36.12798     3.823880
    hsa-miR-338-5p   ENSG00000199153   29.893947      42.21830     8.048856
    hsa-miR-452*                       52.693279    115.58508   15.165157
4

2 回答 2

2

我这样做是出于对@user2806363 无法阅读意义的挫败感。

> mydata1[,1] <- sub("\\*","",mydata1[,1])
> dput(mydata1)
structure(list(Gene_ID = c("hsa-miR-616", "hsa-miR-1296", "hsa-miR-338-5p", 
"hsa-miR-452"), lung.cancer = c(3.653241, 2.688751, 29.893947, 
5.693279), lung.cancer.1 = c(1, 36.12798, 2.2183, 1015.58508), 
    lung.cancer.2 = c(1.838179, 43.82388, 48.048856, 35.165157
    )), .Names = c("Gene_ID", "lung.cancer", "lung.cancer.1", 
"lung.cancer.2"), row.names = c(NA, -4L), class = "data.frame")
> dput(Database)
structure(list(ENS_ID = structure(c(7L, 5L, 3L, 8L, 2L, 1L, 6L, 
4L), .Label = c("ENSG00000199153", "ENSG00000207582", "ENSG00000207800", 
"ENSG00000207804", "ENSG00000207941", "ENSG00000215998", "ENSG00000221263", 
"ENSG00000222831"), class = "factor"), Gene_ID = structure(c(5L, 
7L, 4L, 1L, 2L, 3L, 8L, 6L), .Label = c("hsa-mir-1537", "hsa-mir-30b", 
"hsa-miR-338-5p", "hsa-mir-504", "hsa-mir-548p", "hsa-mir-599", 
"hsa-miR-616", "hsa-mir-935"), class = "factor")), .Names = c("ENS_ID", 
"Gene_ID"), class = "data.frame", row.names = c(NA, -8L))

> merge(mydata1, Database)
         Gene_ID lung.cancer lung.cancer.1 lung.cancer.2          ENS_ID
1 hsa-miR-338-5p   29.893947        2.2183     48.048856 ENSG00000199153
2    hsa-miR-616    3.653241        1.0000      1.838179 ENSG00000207941

> merge(mydata1, Database, all.x=TRUE)
         Gene_ID lung.cancer lung.cancer.1 lung.cancer.2          ENS_ID
1   hsa-miR-1296    2.688751      36.12798     43.823880            <NA>
2 hsa-miR-338-5p   29.893947       2.21830     48.048856 ENSG00000199153
3    hsa-miR-452    5.693279    1015.58508     35.165157            <NA>
4    hsa-miR-616    3.653241       1.00000      1.838179 ENSG00000207941
> 
于 2013-11-05T23:17:33.130 回答
-1

假设你的 class(database) = matrix,class(mydata) = matrix,并且所有列都是字符类,

temp=numeric(nrow(Database))    

for( i in 1:nrow(Database)){
  ind=which(Database[,2]==mydata[,1])

  if(length(ind)!=0){

    temp[ind]=Database[,1][ind]


  }
}
cbind(mydata[,1],temp,mydata[,2],mydata[,3])

将提供您正在寻找的东西

于 2013-11-05T22:56:59.600 回答