下面是我想詢問的問題
我想將data1(col 3) 取代 data 2 (col1)的數值,前提是data1 的 col 1 或 col2 的數值等於 data 2 (col1),我想到的方法只有雙迴圈,有沒有辦法用單迴圈或which( )的方式找出解答?
data 1 (49892 row 3 col )
data 2 (49778 row 3 col )
here is my first approach
loc1=which(outside_data[,3]%in%genes_correspondence[,1])
loc2=which(outside_data[,3]%in%genes_correspondence[,2])
loc=unique(c(loc1, loc2))
outside_data_address=c()
for (i in 1:length(outside_data_overlape)) {
cat('now is handling', i, '-th pw', '\n')
found_match = FALSE
for (j in 1:nrow(genes_correspondence)) {
if (outside_data_overlape[i] %in% genes_correspondence[j, 1:2]) {
outside_data_address[i] <- genes_correspondence[j, 3]
found_match = TRUE
break
}
}
}
cbind(outside_data_address,outside_data[loc,c(5,6,8,9)])
this is the second approach
loc = which(as.character(genes_correspondence_final[,1]) %in% as.character(outside_data_address[,1]) |
as.character(genes_correspondence_final[,2]) %in% as.character(outside_data_address[,1]))
loc2 = which(as.character(outside_data_address[,1]) %in% as.character(genes_correspondence_final[,1]) |
as.character(outside_data_address[,1]) %in% as.character(genes_correspondence_final[,2]))
sample=genes_correspondence_final[loc,] #
sample2=outside_data_address[loc2,] #
sample3=numeric()
i=0
for (i in i+1:6000) {
for (j in i:6500){
cat('now is handling',i,'-th pw','\n')
sample3 = as.matrix(as.character(sample2[i,1])) %in% as.matrix(as.character(sample[j,1:2]))
if (sample3==T){sample2[i,1] =sample[j,3]}
}
if (i == 6000) {break}
}
辛苦您們了