Convert Logical to Dummy Vector in R (Example) | Replace TRUE/FALSE by 0/1 | as.numeric & as.logical

preview_player
Показать описание
R code of this video:

x1 <- c(TRUE, FALSE, FALSE, TRUE, FALSE, TRUE) # Create logical vector
x1 # Print logical vector

x1_dummy # Print dummy vector

x2 <- c("TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "TRUE") # Create character vector
x2 # Print character vector

class(x2) # Check class of vector

x2_logical # Print logical vector

class(x2_logical) # Check class of updated vector

x2_dummy # Print dummy vector

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

Hey, could you please guide me about the following issue? I have the dataset from 2013-2021, but divided into three Excel files as follows: from 2013-2016, from 2017-2019, and from 2020-2021, the Gender variable is a numeric variable taking the value of 1 for "females" and takes the value of 2 if "Male, " also in Excel file there are empty cells which R recognize it as NA. The R only for the first three years of the survey from 2013-to 2016 shows the Gender variable as numeric. Still, the second and third three years, from 2017-to 2019 and from 2020-to 2021, show the Gender variable as logic with NA values in the column of the Gender variable. What can I do to convert that logic into numeric? I tried, in the beginning, to omit the NA values and then change the type of the Gender into numeric, but then I got only the NA and 1 values, which is worse than before. I will appreciate any further help from your side. Thank you.

mar
Автор

Very nice. Still good to know why we would have to do this in the first place. Where is it used and why?

haraldurkarlsson
Автор

Found the solution for Rscript. Need the shebang for Rscript.

bhargavapothakamuri