使变量成为名称

时间:2017-11-18 03:40:11

标签: r function phyloseq

我创造了我的第一个功能,我为自己超级自豪,但我努力让它变得更好。它遍历一个丰富的表,标识每行中最丰富的列,然后给我一个与另一个数据表相关的列的名称,这是使用phyloseq包创建的对象。

find.top.phyla <- function(x){
  require(phyloseq)
  otu <- otu_table(x)
  tax <- tax_table(x)
  j<-apply(otu,1,which.max)
  k <- j[!duplicated(j)]
  l <- data.frame(tax[k,])
  m <- data.frame(otu[,k])
  colnames(m) <- l$Phylum
  n <- colnames(m)[apply(m,1,which.max)]
  m$TopPhyla <- n
  return(m)
}
find.top.phyla(top.pdy.phyl)

这给了我

     Proteobacteria    Actinobacteria    Bacteroidetes    TopPhyla
S1        45                25                10          Proteobacteria
S2        14                35                5           ActinoBacteria
S3        88                19                400         Bacteroidetes

为了使它更有用,我想准确地告诉它我想要哪个分类单元级别,并在数据框中添加适当的丰度以及为每一行中确定的最丰富的分类单元,在分类标准上吐出另一个表格。数据框。如上所示。

find.top.taxa <- function(x,taxa){
  require(phyloseq)
  top.taxa <- tax_glom(x, taxa)
  otu <- otu_table(top.taxa)
  tax <- tax_table(top.taxa)
  j<-apply(otu,1,which.max)
  k <- j[!duplicated(j)]
  l <- data.frame(tax[k,])
  m <- data.frame(otu[,k])
  s <- as.name(taxa)      # This is Where the issue is occuring
  colnames(m) <- l$make.names(taxa)  # This is Where the issue is occuring
  n <- colnames(m)[apply(m,1,which.max)]
  m$make.names(taxa) <- n   # This is Where the issue is occuring
  return(m)

}

我已经确定问题出现在哪里。我已经尝试过&#34; is.name&#34;,&#34; as.name&#34;,&#34; taxa&#34; (它真的不喜欢),还有一些其他的迭代。基本上,我想制作&#34; taxa&#34;变量字符串中的参数,并标识另一个表中的列,其中列与#34; taxa&#34;相同论点。即:find.top.taxa(top.pdy, "Class")和/或find.top.taxa(top.pdy, "Genus")

0 个答案:

没有答案