Coerce Logical (Boolean) Vector to 0 and 1

Coerce logical (boolean) vector to 0 and 1

There are a few ways you can go about this.

  1. Using ifelse

    b <- ifelse(a > 2, 1, 0)

    This is just a simpler way of writing exactly what you've written in the question: if the condition returns TRUE, set the value to 1, otherwise set it to 0.

  2. Using as.numeric

    b <- as.numeric(a > 2)

    Logical values can be converted to their numeric equivalents easily using this function. As one might expect, TRUE is set to 1 and FALSE to 0.

  3. The lazy version of as.numeric

    b <- 1*(a > 2)

    When R sees the multiplication of logical values by a numeric value, it automatically coerces the logicals to their numeric equivalents. Thus logical values can be converted lazily (from a programmer's standpoint) by multiplying by 1.

in R mutate TRUE FALSE to 1 0

Depends slightly on if your data type is truly a boolean, which can be checked via str(). If is a string or logical data type changes the approach a bit. You can use case when but can skip the isTRUE() and mutate() function (though they could work but are not needed). Example below:

# dataframe with column st as string and bol as logical 
x <- data.frame(st = c('TRUE', 'FALSE'), bol = c(TRUE,FALSE))

# see the data type
str(x)

# if string
x$new <- case_when(x$st == 'FALSE' ~ 0,
TRUE ~ 1)
# if logical
x$new_log <- case_when(x$bol == F ~ 0,
TRUE ~ 1)

Additionally, ifelse would work here, too.

Convert dataframe column to 1 or 0 for true/false values and assign to dataframe

@chappers solution (in the comments) works as.integer(as.logical(data.frame$column.name))

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

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

How to convert 'false' to 0 and 'true' to 1?

Use int() on a boolean test:

x = int(x == 'true')

int() turns the boolean into 1 or 0. Note that any value not equal to 'true' will result in 0 being returned.

Boolean vector comparison for TRUE/FALSE single results

Similar to all as suggested in the comment by @akrun, one could take the slightly more "backwards" approach of using any, which evaluates to true if at least a one element in a vector is true -- in your example, one element is the same as the standard with respect to its index and value. By evaluating this (input) with !input, meaning NOT input, you can obtain the result you are after.

# Standard
logical.vector = c(TRUE, TRUE, FALSE, TRUE)

# Test Patterns

# Case 1 (non-match): should be FALSE
!any(logical.vector != c(TRUE, FALSE, TRUE, TRUE))

# Case 2 (non-match): should be FALSE
!any(logical.vector != c(FALSE, TRUE, FALSE, TRUE))

# Case 3 (non-match): should be FALSE
!any(logical.vector != c(TRUE, TRUE, TRUE, TRUE))

# Case 4 (non-match): should be FALSE
!any(logical.vector != c(FALSE, FALSE, FALSE, FALSE))

# Case 5 (match): should be TRUE
!any(logical.vector != c(TRUE, TRUE, FALSE, TRUE))

How can I map True/False to 1/0 in a Pandas DataFrame?

A succinct way to convert a single column of boolean values to a column of integers 1 or 0:

df["somecolumn"] = df["somecolumn"].astype(int)

logical(0) in if statement

logical(0) is a vector of base type logical with 0 length. You're getting this because your asking which elements of this vector equal 0:

> !is.na(c(NA, NA, NA))
[1] FALSE FALSE FALSE
> which(!is.na(c(NA, NA, NA))) == 0
logical(0)

In the next line, you're asking if that zero length vector logical(0) is equal to 0, which it isn't. You're getting an error because you can't compare a vector of 0 length with a scalar.

Instead you could check whether the length of that first vector is 0:

if(length(which(!is.na(c(NA,NA,NA)))) == 0){print('TRUE')}


Related Topics



Leave a reply



Submit