我是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
)调用mydata1
后添加新列,这样,
它会从mydata1
获取基因ID并在Database
中搜索它,如果已找到,请在新列中将其对应ENS ID
添加到mydata1
。
预期输出如下:
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
答案 0 :(得分: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
>
答案 1 :(得分:-1)
假设您的类(数据库)= 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])
将提供您正在寻找的内容