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

前端 未结 6 604
傲寒
傲寒 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

    If there are multiple columns, you could use set (using @josilber's example)

    library(data.table)
    Cols <-  which(sapply(dat, is.logical))
    setDT(dat)
    
    for(j in Cols){
     set(dat, i=NULL, j=j, value= as.numeric(dat[[j]]))
    }
    

提交回复
热议问题