I am exporting data from R with the command:
write.table(output,file = "data.raw", na "-9999", sep = "", row.names = FALSE, col.names = FALSE)
It exports my data correctly, but it exports all of the logical variables as TRUE
and FALSE
.
I need to read the data into another program that can only process numeric values. Is there an efficient way to convert logical
columns to numeric 1s and 0s during the export? I have a large number of numeric variables, so I was hoping to automatically loop through all the variables in the data.table
Alternatively, my output object is a data.table
. Is there an efficient way to convert all the logical variables in a data.table
into numeric variables?
In case it is helpful, here is some code to generate a data.table
with a logical variable in it (it is not a large number of logical variables, but enough to use on example code):
DT = data.table(cbind(1:100, rnorm(100) > 0)
DT[ , V3:= V2 == 1 ]
DT[ , V4:= V2 != 1 ]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…