在R

时间:2016-06-03 20:24:56

标签: r matrix

我有一个逻辑矩阵如下:

     none ants beeswasps grasshoppers flies maggots beetles other
 [1,] T    F    F         F            F     F       F       F    
 [2,] T    F    F         F            F     F       F       F    
 [3,] T    F    F         F            F     F       F       F    
 [4,] T    F    F         F            F     F       F       F    
 [5,] T    F    F         F            F     F       F       F    
 [6,] T    F    F         F            F     F       F       F    
 [7,] F    F    T         F            F     F       T       T    

我想在元素为true时更改具有相应列名称的元素,而不是其他文本。因此,结果矩阵将是:

      none ants beeswasps grasshoppers flies maggots beetles other
 [1,] none                                   
 [2,] none                                   
 [3,] none                                 
 [4,] none                                     
 [5,] none                                   
 [6,] none                               
 [7,]           beeswasps                          beetles    other    

这是一个包含许多T/F值的大矩阵,因此需要一个强大的方法,而不仅仅是创建另一个看起来像我想要的矩阵。

这是一个代码段

C = matrix( 
c(T,T,T,T,T,T,F,F,F,F,F,F,F,F,F,F,F,F,F,F,T,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,T,F,F,F,F,F,F,T), 
  nrow=7, ncol=8) 
colnames(C) <- c("none", "ants", "beeswasps", "grasshoppers", "flies", "maggots", "beetles", "other")

3 个答案:

答案 0 :(得分:2)

我认为这可以胜任:

for (i in colnames(m)){
  m[, i] <- ifelse(m[, i], i, "")
}

更改""以上(例如,使用NA),如果您想要其他内容填写&#34; no entry&#34;例。

以下是模仿矩阵的reproducible示例:

m <- structure(c(FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), .Dim = c(10L, 8L), .Dimnames = list(NULL, c("none", "ants", "beeswasps", "grasshoppers", "flies", "maggots", "beetles", "other")))

m之前:

> m
none  ants beeswasps grasshoppers flies maggots beetles other
[1,] FALSE FALSE     FALSE        FALSE FALSE    TRUE   FALSE FALSE
[2,] FALSE FALSE     FALSE         TRUE FALSE    TRUE   FALSE FALSE
[3,]  TRUE FALSE     FALSE        FALSE  TRUE   FALSE   FALSE FALSE
[4,]  TRUE FALSE     FALSE        FALSE FALSE   FALSE    TRUE FALSE
[5,] FALSE  TRUE     FALSE        FALSE FALSE    TRUE   FALSE FALSE
[6,] FALSE FALSE     FALSE         TRUE  TRUE   FALSE   FALSE FALSE
[7,] FALSE FALSE     FALSE         TRUE FALSE   FALSE   FALSE FALSE
[8,] FALSE FALSE     FALSE        FALSE FALSE   FALSE   FALSE FALSE
[9,]  TRUE FALSE     FALSE        FALSE FALSE   FALSE   FALSE FALSE
[10,] FALSE FALSE     FALSE         TRUE FALSE   FALSE   FALSE FALSE

m之后:

> m
none   ants   beeswasps grasshoppers   flies   maggots   beetles   other
[1,] ""     ""     ""        ""             ""      "maggots" ""        ""   
[2,] ""     ""     ""        "grasshoppers" ""      "maggots" ""        ""   
[3,] "none" ""     ""        ""             "flies" ""        ""        ""   
[4,] "none" ""     ""        ""             ""      ""        "beetles" ""   
[5,] ""     "ants" ""        ""             ""      "maggots" ""        ""   
[6,] ""     ""     ""        "grasshoppers" "flies" ""        ""        ""   
[7,] ""     ""     ""        "grasshoppers" ""      ""        ""        ""   
[8,] ""     ""     ""        ""             ""      ""        ""        ""   
[9,] "none" ""     ""        ""             ""      ""        ""        ""   
[10,] ""     ""     ""        "grasshoppers" ""      ""        ""        ""  

答案 1 :(得分:1)

对于每一行x,您需要结果行ifelse(x, colnames(C), NA)。在apply中对此进行编码会给出:

> t(apply(C, 1, function(x) ifelse(x, colnames(C), NA)))
     none   ants beeswasps   grasshoppers flies maggots beetles   other  
[1,] "none" NA   NA          NA           NA    NA      NA        NA     
[2,] "none" NA   NA          NA           NA    NA      NA        NA     
[3,] "none" NA   NA          NA           NA    NA      NA        NA     
[4,] "none" NA   NA          NA           NA    NA      NA        NA     
[5,] "none" NA   NA          NA           NA    NA      NA        NA     
[6,] "none" NA   NA          NA           NA    NA      NA        NA     
[7,] NA     NA   "beeswasps" NA           NA    NA      "beetles" "other"

答案 2 :(得分:0)

或者你可以做什么完全矢量化(没有任何loopapply家庭功能):

> m
       # none  ants beeswasps grasshoppers flies maggots beetles other
 # [1,] FALSE FALSE     FALSE        FALSE FALSE    TRUE   FALSE FALSE
 # [2,] FALSE FALSE     FALSE         TRUE FALSE    TRUE   FALSE FALSE
 # [3,]  TRUE FALSE     FALSE        FALSE  TRUE   FALSE   FALSE FALSE
 # [4,]  TRUE FALSE     FALSE        FALSE FALSE   FALSE    TRUE FALSE
 # [5,] FALSE  TRUE     FALSE        FALSE FALSE    TRUE   FALSE FALSE
 # [6,] FALSE FALSE     FALSE         TRUE  TRUE   FALSE   FALSE FALSE
 # [7,] FALSE FALSE     FALSE         TRUE FALSE   FALSE   FALSE FALSE
 # [8,] FALSE FALSE     FALSE        FALSE FALSE   FALSE   FALSE FALSE
 # [9,]  TRUE FALSE     FALSE        FALSE FALSE   FALSE   FALSE FALSE
# [10,] FALSE FALSE     FALSE         TRUE FALSE   FALSE   FALSE FALSE

w=which(m==T, arr.ind = TRUE)[,2]
m[m==T]=colnames(m[,w])
m[m==F]=""

> m
      # none   ants   beeswasps grasshoppers   flies   maggots   beetles   other
 # [1,] ""     ""     ""        ""             ""      "maggots" ""        ""   
 # [2,] ""     ""     ""        "grasshoppers" ""      "maggots" ""        ""   
 # [3,] "none" ""     ""        ""             "flies" ""        ""        ""   
 # [4,] "none" ""     ""        ""             ""      ""        "beetles" ""   
 # [5,] ""     "ants" ""        ""             ""      "maggots" ""        ""   
 # [6,] ""     ""     ""        "grasshoppers" "flies" ""        ""        ""   
 # [7,] ""     ""     ""        "grasshoppers" ""      ""        ""        ""   
 # [8,] ""     ""     ""        ""             ""      ""        ""        ""   
 # [9,] "none" ""     ""        ""             ""      ""        ""        ""   
# [10,] ""     ""     ""        "grasshoppers" ""      ""        ""        ""