Replace logical values (TRUE / FALSE) with numeric (1 / 0)

前端 未结 6 597
傲寒
傲寒 2020-12-03 04:34

I am exporting data from R with the command:

write.table(output,file = "data.raw", na "-9999", sep = "\\t", row.names = FALSE, c         


        
6条回答
  •  渐次进展
    2020-12-03 05:14

    Simplest way of doing this!

    Multiply your matrix by 1

    For example:

    A <- matrix(c(TRUE,FALSE,TRUE,TRUE,TRUE,FALSE,FALSE,TRUE),ncol=4)
    A
    
    #       [,1] [,2]  [,3]  [,4]
    # [1,]  TRUE TRUE  TRUE FALSE
    # [2,] FALSE TRUE FALSE  TRUE
    
    B <- 1*A
    B
    #      [,1] [,2] [,3] [,4]
    # [1,]    1    1    1    0
    # [2,]    0    1    0    1
    

    (You could also add zero: B <- 0 + A)

提交回复
热议问题