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
converting logical true/false to numeric 1/0?
You can write (if x > param then 1 else 0)*f(x)
for example.
You can also write charfun(x > param)*f(x)
. See ? charfun
.
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))
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)
Converting all occurrence of True/False to 1/0 in a dataframe with mixed datatype
applymap
is not in-place by default, it will return a new dataframe.
The correct way:
test = test.applymap(lambda x: 1 if x == True else x)
test = test.applymap(lambda x: 0 if x == False else x)
or
test = test.applymap(lambda x: 1 if x == True else x).test.applymap(lambda x: 0 if x=='False' else x)
or simply
test.applymap(lambda x: 1 if x == True else x, inplace=True)
test.applymap(lambda x: 0 if x == False else x, inplace=True)
Although replace
seems the best way of achieving this:
test.replace(False, 0, inplace=True)
Change 0/1 values to TRUE/FALSE in R
Use x1 == 1
:
> x1 == 1
[,1] [,2]
[1,] TRUE TRUE
[2,] FALSE TRUE
>
Or use x1 == TRUE
:
> x1 == TRUE
[,1] [,2]
[1,] TRUE TRUE
[2,] FALSE TRUE
>
Or with apply
and as.logical
:
> apply(x1, 2, as.logical)
[,1] [,2]
[1,] TRUE FALSE
[2,] TRUE TRUE
>
Change values in df to 0 = FALSE, 1 = TRUE, 2 = TRUE
You can use mutate_if
to change numeric columns to their logical equivalents:
test %>% mutate_if(is.numeric,as.logical)
ID Primary Secondary Tertiary
1 A FALSE TRUE TRUE
2 B FALSE FALSE TRUE
3 C TRUE TRUE FALSE
4 D TRUE TRUE FALSE
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.
Related Topics
How to Match by Nearest Date from Two Data Frames
Deploying R Shiny App as a Standalone Application
How to Get a Barplot with Several Variables Side by Side Grouped by a Factor
How to Determine If You Have an Internet Connection in R
Reason Behind Speed of Fread in Data.Table Package in R
Error in New.Session():Could Not Establish Session After 5 Attempts
Sort a String of Comma-Separated Items Alphabetically
Merging Rows with the Same Id Variable
What's the Difference Between '=' and '<-' in R
Imported a CSV-Dataset to R But the Values Becomes Factors
Randomly Insert Nas into Dataframe Proportionaly
Passing Several Arguments to Fun of Lapply (And Others *Apply)
Rmarkdown: How to End Tabbed Content
How to Use Tidyr::Separate When the Number of Needed Variables Is Unknown
Change Day of the Month in a Date to First Day (01)
How to Read CSV File in R Where Some Values Contain the Percent Symbol (%)