在R中如何建立动态的变量名,或者动态的文件名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在R中如何建立动态的变量名,或者动态的文件名相关的知识,希望对你有一定的参考价值。

参考技术A 格式:dispose(指针变量)例如:dispose(p)

R 动态定义变量名 assign

rm(list=ls())

library(GSVA)
library(GSEABase)
library(GSVAdata)
library(msigdbr)
library(org.Hs.eg.db)
library(Seurat)
library(Rtsne)
setwd("/heartdata8t_A/zhangpeng/Final.results/Final_Project_III/GSVA")

### Merging the cannicalc2BroadSets
data(c2BroadSets)
canonicalC2BroadSets <- c2BroadSets[c(grep("^KEGG", names(c2BroadSets)),
                                      grep("^REACTOME", names(c2BroadSets)),
                                      grep("^BIOCARTA", names(c2BroadSets)))]
## Adding Hallmark genesets
m_df = msigdbr(species = "Homo sapiens", category = "H")
gset.all <- unique(m_df$gs_name)

for(geneset_i in 1:length(gset.all))
  names_geneset_i <- gset.all[geneset_i]
  entrez_gene_geneset_i <- as.character(unique(m_df$entrez_gene[which(m_df$gs_name == gset.all[geneset_i])]))
  HallMarks <- GeneSet(entrez_gene_geneset_i, geneIdType=EntrezIdentifier(),
                 collectionType=BroadCollection(category="c2"), setName=names_geneset_i)
  canonicalC2BroadSets <- GeneSetCollection(c(canonicalC2BroadSets, HallMarks))


## Adding human-curated genesets (GEO/Cancer)
load("GC.signature.Rdata")
GC.signature <- as.character(mapIds(org.Hs.eg.db, GC.signature, ENTREZID, SYMBOL))
GC.signature <- GeneSet(GC.signature, geneIdType=EntrezIdentifier(),
                     collectionType=BroadCollection(category="c2"), setName="GC.signature")
canonicalC2BroadSets <- GeneSetCollection(c(canonicalC2BroadSets, GC.signature))

load("GC.signature.literature.Rdata")
GC.signature.literature <- as.character(mapIds(org.Hs.eg.db, GC.signature.literature, ENTREZID, SYMBOL))
GC.signature.literature <- GeneSet(GC.signature.literature, geneIdType=EntrezIdentifier(),
                        collectionType=BroadCollection(category="c2"), setName="GC.signature.literature")
canonicalC2BroadSets <- GeneSetCollection(c(canonicalC2BroadSets, GC.signature.literature))


# Computing the GSVA score based the above curated datasets
load("subset.epithelial.cells.gc.signature.Rdata")
processed.data <- as.matrix(epithelial.cells.re.subset.GC.signature@assays$SCT@counts)

rownames.processed.data <- rownames(processed.data)
entrez.rownames.processed.data <- as.character(mapIds(org.Hs.eg.db, rownames.processed.data , ENTREZID, SYMBOL))

invalid.index <- which(is.na(entrez.rownames.processed.data))
processed.data <- processed.data[-invalid.index,]
rownames(processed.data) <- entrez.rownames.processed.data[-invalid.index]


esmicro.processed <- gsva(processed.data, canonicalC2BroadSets, min.sz=5, max.sz=500,
                          mx.diff=TRUE, verbose=FALSE, parallel.sz=1, kcdf = "Poisson")
## Results
# library(pheatmap)
# esmicro.processed <- esmicro.processed[grep("KEGG",rownames(esmicro.processed)),]
# pheatmap(esmicro.processed)

save(esmicro.processed,file = "esmicro.processed.Rdata")
tsne <- Rtsne(esmicro.processed,dims = 2)
plot(tsne$Y)


epi<-epithelial.cells.re@meta.data
bb<-epi$SCT_snn_res.1[which(epi$SCT_snn_res.1 ==5)]  

bb<-epi[which(epi[,12]==5),]
cc<-epi[which(epi[,12]==30),]
aa<-rbind(bb,cc)
g1.index<-rownames(aa)
pro<- esmicro.processed
g1<-pro[ , which(colnames(pro) %in% g1.index )]
g2<-pro[ , -which(colnames(pro) %in% g1.index )]


heatmap(pro)







pathway_name = rownames(g1)
tm <- list(P-value = c(), Pathway_name = c())



cell.types <- unique(Idents(epithelial.cells.re))


library(pryr)
#setClass("All_results", slots = list(C0_results = "data.frame"))
#all_results <- new("All_results", C0_results = tm)

cell.types

#for(i in 1:length(cell.types))
#  print((as.numeric(cell.types[i])))
  #name = paste0("tm_",as.numeric(cell.types[i]))
  #eval(parse( name )   )<- list(P-value = c(), Pathway_name = c())  
  #append(all_ans,list(name = c()))
  
#

for(j in cell.types)
  cc<-epi[which(epi[,12]==j),]
  index<-rownames(cc)
  g1<-pro[ , which(colnames(pro) %in% index )]
  pathway_name = rownames(g1)
  g2<-pro[ , -which(colnames(pro) %in% index )]
  tm <- list(P-value = c(), Pathway_name = c()) 
 ## t.test
 for(i in 1:dim(g1)[1])
   results<- t.test(g1[i,],g2[i,])$p.value
   tm$`P-value`<-append(tm$`P-value`,results)
   tm$Pathway_name<- append(tm$Pathway_name,pathway_name[i])
 
  assign(paste("tm_",j,sep = ""),tm)


print(results)
 
 
  data.frame()
  p.val,pathway_name
  
  result[[celltype_i]] <- data.frame 
  



dftm<- data.frame(tm)
dftm <- dftm[sort(dftm$P.value,index.return=TRUE)$ix,]







down <- sample(colnames(pro),round(nrow(pro)/5))
a<-pro[,down]
heatmap(a)





raw.data

result <- list()

cell.types <- unique(Idents(epithelial.cells.re))

for(celltype_i in cell.types)
  
  ## t.test
  
   data.frame()
  p.val,pathway_name

  result[[celltype_i]] <- data.frame 
  
  
  
  
  
  

主要部分“

 1 for(j in cell.types)
 2   cc<-epi[which(epi[,12]==j),]
 3   index<-rownames(cc)
 4   g1<-pro[ , which(colnames(pro) %in% index )]
 5   pathway_name = rownames(g1)
 6   g2<-pro[ , -which(colnames(pro) %in% index )]
 7   tm <- list(P-value = c(), Pathway_name = c()) 
 8  ## t.test
 9  for(i in 1:dim(g1)[1])
10    results<- t.test(g1[i,],g2[i,])$p.value
11    tm$`P-value`<-append(tm$`P-value`,results)
12    tm$Pathway_name<- append(tm$Pathway_name,pathway_name[i])
13  
14   assign(paste("tm_",j,sep = ""),tm)
15 
16 
17 print(results)
18  

 

以上是关于在R中如何建立动态的变量名,或者动态的文件名的主要内容,如果未能解决你的问题,请参考以下文章

如何在 shell 脚本中动态生成新的变量名?

EXCEL VBA 里面如何动态获取字符串转换为变量名?

R 动态定义变量名 assign

linux下的动态链接库管理

linux中建立自己的静态库 动态库并使用

如何访问树枝中的动态变量名?