Skip to contents

Seamlessly convert a yes or no to either a binary or logical output

Usage

yesNoBool(
  table,
  fldname,
  out = c("change", "append", "vector"),
  type = c("bin", "log")
)

Arguments

table

data frame

fldname

field name in the data frame

out

output form, choices - change, append, vector

type

output type, choices - bin, log

Value

converted Yes/No entries into 1/0 or TRUE/FALSE

Details

type - "bin" for binary, and "log" for logical

Examples

# \donttest{
# Declare data for example
usedata <- data.frame(ID = 1:32)
usedata #view the dataset
#>    ID
#> 1   1
#> 2   2
#> 3   3
#> 4   4
#> 5   5
#> 6   6
#> 7   7
#> 8   8
#> 9   9
#> 10 10
#> 11 11
#> 12 12
#> 13 13
#> 14 14
#> 15 15
#> 16 16
#> 17 17
#> 18 18
#> 19 19
#> 20 20
#> 21 21
#> 22 22
#> 23 23
#> 24 24
#> 25 25
#> 26 26
#> 27 27
#> 28 28
#> 29 29
#> 30 30
#> 31 31
#> 32 32

usedata$yess = rep(c("yes","n","no","YES","No","NO","yES","Y"),4) #create a new column
usedata #view the modified dataset
#>    ID yess
#> 1   1  yes
#> 2   2    n
#> 3   3   no
#> 4   4  YES
#> 5   5   No
#> 6   6   NO
#> 7   7  yES
#> 8   8    Y
#> 9   9  yes
#> 10 10    n
#> 11 11   no
#> 12 12  YES
#> 13 13   No
#> 14 14   NO
#> 15 15  yES
#> 16 16    Y
#> 17 17  yes
#> 18 18    n
#> 19 19   no
#> 20 20  YES
#> 21 21   No
#> 22 22   NO
#> 23 23  yES
#> 24 24    Y
#> 25 25  yes
#> 26 26    n
#> 27 27   no
#> 28 28  YES
#> 29 29   No
#> 30 30   NO
#> 31 31  yES
#> 32 32    Y

# Set all yess field as standardize boolean
# Task: convert the "yess" column content to 1/0 or TRUE/FALSE
# Notice that you have add the column name with or without quotes
yesNoBool(usedata,yess, type="bin") #set all as binary 1/0
#>    ID yess
#> 1   1    1
#> 2   2    0
#> 3   3    0
#> 4   4    1
#> 5   5    0
#> 6   6    0
#> 7   7    1
#> 8   8    1
#> 9   9    1
#> 10 10    0
#> 11 11    0
#> 12 12    1
#> 13 13    0
#> 14 14    0
#> 15 15    1
#> 16 16    1
#> 17 17    1
#> 18 18    0
#> 19 19    0
#> 20 20    1
#> 21 21    0
#> 22 22    0
#> 23 23    1
#> 24 24    1
#> 25 25    1
#> 26 26    0
#> 27 27    0
#> 28 28    1
#> 29 29    0
#> 30 30    0
#> 31 31    1
#> 32 32    1
yesNoBool(usedata,"yess", type="log") #set all as logical TRUE/FALSE
#>    ID  yess
#> 1   1  TRUE
#> 2   2 FALSE
#> 3   3 FALSE
#> 4   4  TRUE
#> 5   5 FALSE
#> 6   6 FALSE
#> 7   7  TRUE
#> 8   8  TRUE
#> 9   9  TRUE
#> 10 10 FALSE
#> 11 11 FALSE
#> 12 12  TRUE
#> 13 13 FALSE
#> 14 14 FALSE
#> 15 15  TRUE
#> 16 16  TRUE
#> 17 17  TRUE
#> 18 18 FALSE
#> 19 19 FALSE
#> 20 20  TRUE
#> 21 21 FALSE
#> 22 22 FALSE
#> 23 23  TRUE
#> 24 24  TRUE
#> 25 25  TRUE
#> 26 26 FALSE
#> 27 27 FALSE
#> 28 28  TRUE
#> 29 29 FALSE
#> 30 30 FALSE
#> 31 31  TRUE
#> 32 32  TRUE


# Task: By default, the 'out' argument is set to "change"
# means that the original data field will be
# replaced with the results as above

# In this example, set the out variable to
# append data frame with a new column name containing the result

yesNoBool(usedata,yess,"append")
#>    ID yess new__col
#> 1   1  yes        1
#> 2   2    n        0
#> 3   3   no        0
#> 4   4  YES        1
#> 5   5   No        0
#> 6   6   NO        0
#> 7   7  yES        1
#> 8   8    Y        1
#> 9   9  yes        1
#> 10 10    n        0
#> 11 11   no        0
#> 12 12  YES        1
#> 13 13   No        0
#> 14 14   NO        0
#> 15 15  yES        1
#> 16 16    Y        1
#> 17 17  yes        1
#> 18 18    n        0
#> 19 19   no        0
#> 20 20  YES        1
#> 21 21   No        0
#> 22 22   NO        0
#> 23 23  yES        1
#> 24 24    Y        1
#> 25 25  yes        1
#> 26 26    n        0
#> 27 27   no        0
#> 28 28  YES        1
#> 29 29   No        0
#> 30 30   NO        0
#> 31 31  yES        1
#> 32 32    Y        1
#or yesNoBool(usedata,"yess","append")

# In this example, return as vector
yesNoBool(usedata,yess,"vector")
#>  [1] 1 0 0 1 0 0 1 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 1 1
#> Levels: 0 1
#or yesNoBool(usedata,"yess","vector")

# Task: Return result as logical
yesNoBool(usedata,"yess",type = "log")
#>    ID  yess
#> 1   1  TRUE
#> 2   2 FALSE
#> 3   3 FALSE
#> 4   4  TRUE
#> 5   5 FALSE
#> 6   6 FALSE
#> 7   7  TRUE
#> 8   8  TRUE
#> 9   9  TRUE
#> 10 10 FALSE
#> 11 11 FALSE
#> 12 12  TRUE
#> 13 13 FALSE
#> 14 14 FALSE
#> 15 15  TRUE
#> 16 16  TRUE
#> 17 17  TRUE
#> 18 18 FALSE
#> 19 19 FALSE
#> 20 20  TRUE
#> 21 21 FALSE
#> 22 22 FALSE
#> 23 23  TRUE
#> 24 24  TRUE
#> 25 25  TRUE
#> 26 26 FALSE
#> 27 27 FALSE
#> 28 28  TRUE
#> 29 29 FALSE
#> 30 30 FALSE
#> 31 31  TRUE
#> 32 32  TRUE
# }