Skip to contents

Convert Yes/No to 1/0 or to TRUE/FALSE or vice versa

Usage

as.boolean(ds, type = 3)

Arguments

ds

item to convert

type

format to convert to, choices 1, 2 or 3

Value

output adhering to the format of the type provided

Details

Output various format of booleans into a specified format. Below are the options for the type argument.

type: options are as follows -

1 - Yes/No
2 - TRUE/FALSE
3 - 1/0

Examples


# Task: convert "yes" or "no" to format of TRUE or FALSE
as.boolean("yes",2)
#> [1] TRUE
as.boolean("no",2)
#> [1] FALSE
as.boolean("YES",2)
#> [1] TRUE
as.boolean("NO",2)
#> [1] FALSE

# Task: convert "yes" or "no" to format of 1 or 0
as.boolean("yes",3)
#> [1] 1
#> Levels: 1
as.boolean("no",3)
#> [1] 0
#> Levels: 0
as.boolean("YES",3)
#> [1] 1
#> Levels: 1
as.boolean("NO",3)
#> [1] 0
#> Levels: 0


# Task: convert 1 to format of Yes or No
as.boolean(1,1)
#> [1] Yes
#> Levels: Yes

# Task: convert "T" to format of Yes or No
as.boolean("T",1)
#> [1] Yes
#> Levels: Yes


# Task: convert "f" to format of TRUE or FALSE
as.boolean("f",2)
#> [1] FALSE


# Task: convert 1 to format of TRUE or FALSE
as.boolean(1,2)
#> [1] TRUE


# Task: convert "Y" or "y" to format of Yes or No
as.boolean("Y",1) #uppercase Y
#> [1] Yes
#> Levels: Yes
as.boolean("y",1) #lowercase y
#> [1] Yes
#> Levels: Yes


# Task: convert TRUE/FALSE to format of 1 or 0
as.boolean(TRUE,3)
#> [1] 1
#> Levels: 1
as.boolean(FALSE,3)
#> [1] 0
#> Levels: 0


# Task: convert TRUE/FALSE to format of Yes or No
as.boolean(TRUE,1)
#> [1] Yes
#> Levels: Yes
as.boolean(FALSE,1)
#> [1] No
#> Levels: No



# In case of error in argument
# as.boolean("tr",3) #NA
# as.boolean("ye",3) #NA

# vector of mixed boolean to TRUE/FALSE or 1/0
multv <- c(TRUE,"y","n","YES","yes",FALSE,"f","F","T","t")
as.boolean(multv,1) # return vector as Yes/No
#>  [1] Yes Yes No  Yes Yes No  No  No  Yes Yes
#> Levels: No Yes
as.boolean(multv,2) # return vector as TRUE/FALSE
#>  [1]  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE
as.boolean(multv,3) # return vector as 1/0
#>  [1] 1 1 0 1 1 0 0 0 1 1
#> Levels: 0 1