Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am exporting data from R with the command:

write.table(output,file = "data.raw", na "-9999", sep = "\t", 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 ] 
like image 437
johneric Avatar asked Jun 19 '15 16:06

johneric


People also ask

How do I change logical to numeric in R?

R – Convert Logical Vector into Integer Vector To convert a given logical vector into integer vector in R, call as. integer() function and pass the logical vector as argument to this function. as. integer() returns a new vector with the logical values transformed into integer values.

How do I use numeric function in R?

To convert factors to the numeric value in R, use the as. numeric() function. If the input is a vector, then use the factor() method to convert it into the factor and then use the as. numeric() method to convert the factor into numeric values.

What is as logical in R?

as. logical() function in R Language is used to convert an object to a logical vector.


1 Answers

For a data.frame, you could convert all logical columns to numeric with:

# The data set.seed(144) dat <- data.frame(V1=1:100,V2=rnorm(100)>0) dat$V3 <- dat$V2 == 1 head(dat) #   V1    V2    V3 # 1  1 FALSE FALSE # 2  2  TRUE  TRUE # 3  3 FALSE FALSE # 4  4 FALSE FALSE # 5  5 FALSE FALSE # 6  6  TRUE  TRUE  # Convert all to numeric cols <- sapply(dat, is.logical) dat[,cols] <- lapply(dat[,cols], as.numeric) head(dat) #   V1 V2 V3 # 1  1  0  0 # 2  2  1  1 # 3  3  0  0 # 4  4  0  0 # 5  5  0  0 # 6  6  1  1 

In data.table syntax:

# Data set.seed(144) DT = data.table(cbind(1:100,rnorm(100)>0)) DT[,V3 := V2 == 1] DT[,V4 := FALSE] head(DT) #    V1 V2    V3    V4 # 1:  1  0 FALSE FALSE # 2:  2  1  TRUE FALSE # 3:  3  0 FALSE FALSE # 4:  4  0 FALSE FALSE # 5:  5  0 FALSE FALSE # 6:  6  1  TRUE FALSE  # Converting (to.replace <- names(which(sapply(DT, is.logical)))) # [1] "V3" "V4" for (var in to.replace) DT[, (var):= as.numeric(get(var))] head(DT) #    V1 V2 V3 V4 # 1:  1  0  0  0 # 2:  2  1  1  0 # 3:  3  0  0  0 # 4:  4  0  0  0 # 5:  5  0  0  0 # 6:  6  1  1  0 
like image 130
josliber Avatar answered Oct 13 '22 08:10

josliber