数据类型的转换2021-01-28

在网络上 找到的代码

###先下载对应关系,得到一个个单独的表格
rm(list = ls())
####################################
#download miRNA_RNA_interaction
####################################
family=read.table("hg19_all_fimaly.txt",sep="\t")

miRNA=unlist(strsplit(as.character(family$V4),","))
class(miRNA)
dir.create("mRNA")
for(mir in miRNA){
  file=paste("mRNA/",mir,".txt",sep="")
  link=paste("http://starbase.sysu.edu.cn/api/miRNATarget/?assembly=hg19&geneType=mRNA&miRNA=",mir,"&clipExpNum=1°raExpNum=0&pancancerNum=0&programNum=1&program=None&target=all",sep="")
  download.file(link,file)
  
  Sys.sleep(5)}

####################################
#download lncRNA_miRNA_interaction
####################################
family=read.table("hg19_all_fimaly.txt",sep="\t")
miRNA=unlist(strsplit(as.character(family$V4),","))
dir.create("lncRNA")
for(mir in miRNA){
  file=paste("lncRNA/",mir,".txt",sep="")
  link=paste("http://starbase.sysu.edu.cn/api/miRNATarget/?assembly=hg19&geneType=lncRNA&miRNA=",mir,"&clipExpNum=1°raExpNum=0&pancancerNum=0&programNum=1&program=None&target=all",sep="")
  download.file(link,file)
  Sys.sleep(5)
}
####################################
#download circRNA_miRNA_interaction
####################################
dir.create("circRNA")
for(mir in miRNA){
  file=paste("circRNA/",mir,".txt",sep="")
  link=paste("http://starbase.sysu.edu.cn/api/miRNATarget/?assembly=hg19&geneType=circRNA&miRNA=",mir,"&clipExpNum=1°raExpNum=0&pancancerNum=0&programNum=1&program=None&target=all",sep="")
  download.file(link,file)
  Sys.sleep(5)
}


#把单独的表格合并成1个对应关系表格

###########################################
#combine lncRNA_miRNA_interaction
###########################################
lncRNA_files <- list.files(path="lncRNA", full.names=TRUE)

library(plyr)
lncRNA.list <- llply(lncRNA_files, function(x)read.table(x,header=T,sep="\t",comment.char ="#",stringsAsFactors=F))
combind_lncRNA=do.call(rbind,lncRNA.list)
write.table(file="lncRNA_miRNA_interaction.txt",combind_lncRNA,quote=F,sep="\t",row.names = F)

##################################
#combine circRNA_miRNA_interaction
##################################
circRNA_files <- list.files(path="circRNA", full.names=TRUE)

library(plyr)
circRNA.list <- llply(circRNA_files, function(x)read.table(x,header=T,sep="\t",comment.char ="#",stringsAsFactors=F))
combind_circRNA=do.call(rbind,circRNA.list)
write.table(file="circRNA_miRNA_interaction.txt",combind_circRNA,quote=F,sep="\t",row.names = F)
##################################
#combine mRNA_miRNA_interaction
##################################
rm(list=ls())
mRNA_files <- list.files(path="mRNA", full.names=TRUE)


library(plyr)
mRNA.list <- llply(mRNA_files, function(x)read.table(x,header=T,sep="\t",comment.char ="#",stringsAsFactors=F))
combind_mRNA=do.call(rbind,mRNA.list)
write.table(file="mRNA_miRNA_interaction.txt",combind_mRNA,quote=F,sep="\t",row.names = F)

然后自己想利用这个代码,想着输入的不是所以的miRNA嘛,那我只需要下载自己手里的miRNA就好了啊。
于是把自己的差异miRNA存在一个文档里,读取进去后却一直不成功,只下载了第一个。
后来用class()看到原来人家代码输进去的是字符型向量,是一个一个miRNA输进去下载的,而我这个读取表格是数据框,所以一直不成功。
之后想当然的以为把数据类型改成字符型,as.character(miRNA),就可以了,结果也不行,还是数据框,只不过里面的数据类型变成了字符型而已。
后来想着降维,最终得到下面的代码,先把数据框转变成矩阵,再把矩阵转变成向量,就好了。

miRNA=read.table("DemiRNA_GSE125998.txt",sep="\t")
miRNA<-as.matrix(miRNA)
miRNA<-as.character(miRNA)

你可能感兴趣的:(数据类型的转换2021-01-28)