- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a blank column with randomization in an R data frame?
To create a blank column with randomization in an R data frame, we can use sample function and pass the blanks with single space. For example, if we want to create a vector say x that will be added in the data frame can be created by using the command −
x<-sample(c(1," "),20,replace=TRUE)
Check out the below examples to understand how we can create the data frame with columns having blank column.
Example1
ID<-1:20 x<-sample(c(1," ",3),20,replace=TRUE) df1<-data.frame(ID,x) df1
Output
ID x 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 6 7 7 8 8 3 9 9 10 10 1 11 11 1 12 12 3 13 13 1 14 14 1 15 15 1 16 16 1 17 17 3 18 18 3 19 19 1 20 20
Example2
S.No<-1:20 y<-sample(c(1," "),20,replace=TRUE) df2<-data.frame(S.No,y) df2
Output
S.No y 1 1 1 2 2 3 3 1 4 4 5 5 1 6 6 7 7 1 8 8 1 9 9 1 10 10 1 11 11 1 12 12 13 13 1 14 14 1 15 15 16 16 17 17 1 18 18 19 19 1 20 20
- Related Articles
- How to create a column in an R data frame with cumulative sum?
- How to create a lagged column in an R data frame?
- How to create a group column in an R data frame?
- How to create a duplicate column in an R data frame with different name?
- Create an integer column in an R data frame with leading zeros
- How to concatenate column values and create a new column in an R data frame?
- How to create a boxplot of single column in R data frame with column name?
- How to create a row at the end an R data frame with column totals?
- How to create histogram for discrete column in an R data frame?
- How to create a data frame with a column having repeated values in R?
- How to create a new column with a subset of row sums in an R data frame?
- How to create a date vector with randomization in R?
- How to create a column with largest size string value in rows in an R data frame?
- How to create a column with the serial number of values in character column of an R data frame?
- How to create a column with column name for maximum value in each row of an R data frame?

Advertisements