How to Construct a Data Frame with Dummy Variables in R (Example Code)
In this article, I’ll show how to initialize a data frame with dummies in the R programming language.
Example: How to Create a Dummy Data Frame
set.seed(2867754) # Setting a random seed my_df <- data.frame(matrix(rbinom(100, 1, 0.5), nrow = 10)) # Creating dummy data frame my_df # Displaying dummy data frame # X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 # 1 1 1 0 1 0 1 1 0 1 1 # 2 0 0 0 0 0 0 1 0 1 1 # 3 1 1 0 0 0 0 1 0 1 0 # 4 1 0 1 0 0 0 1 1 0 1 # 5 0 1 0 0 0 0 1 1 1 1 # 6 0 0 1 0 1 1 0 1 0 1 # 7 0 0 1 1 1 0 0 0 0 1 # 8 0 1 1 0 1 0 0 0 0 1 # 9 0 0 1 1 0 0 1 1 0 1 # 10 0 1 1 1 1 0 1 1 0 0 |
set.seed(2867754) # Setting a random seed my_df <- data.frame(matrix(rbinom(100, 1, 0.5), nrow = 10)) # Creating dummy data frame my_df # Displaying dummy data frame # X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 # 1 1 1 0 1 0 1 1 0 1 1 # 2 0 0 0 0 0 0 1 0 1 1 # 3 1 1 0 0 0 0 1 0 1 0 # 4 1 0 1 0 0 0 1 1 0 1 # 5 0 1 0 0 0 0 1 1 1 1 # 6 0 0 1 0 1 1 0 1 0 1 # 7 0 0 1 1 1 0 0 0 0 1 # 8 0 1 1 0 1 0 0 0 0 1 # 9 0 0 1 1 0 0 1 1 0 1 # 10 0 1 1 1 1 0 1 1 0 0
Further Resources & Related Articles
Have a look at the following list of R programming language tutorials. They illustrate similar topics as this post: