【问题标题】:Replace values in data frame from column of indexes从索引列替换数据框中的值
【发布时间】:2018-01-31 03:07:22
【问题描述】:

我有一个如下所示的数据矩阵:

> taxmat = matrix(sample(letters, 70, replace = TRUE), nrow = 10, ncol = 7)
> rownames(taxmat) <- paste0("OTU", 1:nrow(taxmat))
> taxmat<-cbind(taxmat,c("Genus","Genus","Genus","Family","Family","Order","Genus","Species","Genus","Species"))
> colnames(taxmat) <- c("Domain", "Phylum", "Class", "Order", "Family", "Genus", "Species", "Lowest")
> taxmat
      Domain Phylum Class Order Family Genus Species Lowest   
OTU1  "h"    "c"    "q"   "e"   "q"    "w"   "v"     "Genus"  
OTU2  "f"    "y"    "q"   "z"   "p"    "w"   "v"     "Genus"  
OTU3  "w"    "q"    "i"   "i"   "z"    "j"   "f"     "Genus"  
OTU4  "c"    "e"    "f"   "n"   "z"    "b"   "d"     "Family" 
OTU5  "g"    "w"    "q"   "k"   "e"    "x"   "k"     "Family" 
OTU6  "x"    "j"    "l"   "w"   "z"    "o"   "q"     "Order"  
OTU7  "k"    "s"    "j"   "y"   "t"    "a"   "t"     "Genus"  
OTU8  "w"    "u"    "s"   "w"   "g"    "y"   "n"     "Species"
OTU9  "t"    "r"    "t"   "o"   "i"    "l"   "z"     "Genus"  
OTU10 "x"    "p"    "j"   "f"   "k"    "q"   "w"     "Species"

“最低”列告诉我我对该行数据有信心的最低排名。对于每一行,我想将“最低”指示的列后面的列中的值替换为“未知”。

此示例的预期输出为:

       Domain Phylum Class Order Family   Genus     Species       Lowest
 OTU1  "b"    "b"    "v"   "v"   "l"      "n"       "unknown"     "Genus"
 OTU2  "l"    "m"    "w"   "b"   "f"      "y"       "unknown"     "Genus"
 OTU3  "h"    "w"    "n"   "y"   "k"      "f"       "unknown"     "Genus"
 OTU4  "u"    "m"    "p"   "n"   "t"      "unknown" "unknown"     "Family"
 OTU5  "o"    "b"    "q"   "w"   "a"      "unknown" "unknown"     "Family"
 OTU6  "s"    "j"    "l"   "d"   "unknown""unknown" "unknown"     "Order"
 OTU7  "v"    "y"    "t"   "p"   "s"      "v"       "unknown"     "Genus"
 OTU8  "b"    "r"    "k"   "d"   "q"      "c"       "q"           "Species"
 OTU9  "k"    "h"    "b"   "w"   "h"      "x"       "unknown"     "Genus"
 OTU10 "o"    "p"    "b"   "n"   "k"      "d"       "q"           "Species"

我可以将所有索引替换为向量

idx<-lapply(tax$Lowest, grep, colnames(tax))
idx <- as.numeric(unlist(idx))+1

但我不确定如何替换这些值。感谢您的帮助!

【问题讨论】:

标签: r


【解决方案1】:

我们可以使用循环遍历apply 的行,并通过match 将列的names 与最后一个元素的列(即“最低”中的元素)创建一个逻辑索引replace 的值'未知'的行

t(apply(m1, 1, function(x) {
         i1 <- match( x[8], names(x)[-8])+1
         i1[i1>7] <- 0
         i1 <- if(i1!=0) i1:7 else i1
        c(replace(x[-8], i1, "unknown"), x[8])}))
#      Domain Phylum Class Order Family    Genus     Species   Lowest   
#OTU1  "b"    "b"    "v"   "v"   "l"       "n"       "unknown" "Genus"  
#OTU2  "l"    "m"    "w"   "b"   "f"       "y"       "unknown" "Genus"  
#OTU3  "h"    "w"    "n"   "y"   "k"       "f"       "unknown" "Genus"  
#OTU4  "u"    "m"    "p"   "n"   "t"       "unknown" "unknown" "Family" 
#OTU5  "o"    "b"    "q"   "w"   "a"       "unknown" "unknown" "Family" 
#OTU6  "s"    "j"    "l"   "d"   "unknown" "unknown" "unknown" "Order"  
#OTU7  "v"    "y"    "t"   "p"   "s"       "v"       "unknown" "Genus"  
#OTU8  "b"    "r"    "k"   "d"   "q"       "c"       "q"       "Species"
#OTU9  "k"    "h"    "b"   "w"   "h"       "x"       "unknown" "Genus"  
#OTU10 "o"    "p"    "b"   "n"   "k"       "d"       "q"       "Species"

或者另一种选择是基于列名的match 创建一个行/列索引,最后一列为'm1'和行序列,然后cbind 索引并分配'm1 中的值'到'未知'

lst <- Map(function(x, y) if(x >y) 0 else x:y, match(m1[,8], colnames(m1)[-8])+1, 7)
m1[cbind(rep(seq_len(nrow(m1)), lengths(lst)), unlist(lst))] <- "unknown"

【讨论】:

    猜你喜欢
    • 2017-12-26
    • 2013-12-28
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2020-10-23
    相关资源
    最近更新 更多