- 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 generate random numbers with sequence and store in a data frame column in R?
To generate random numbers with sequence and storing them in a data frame column, we can use sample function with seq. For example, if we want to create a data frame column having random sequence of values of size 50 between 1 to 100 by considering every tenth value then we can use the command
df<-data.frame(x=sample(seq(from=1,to=100,by=10),size=50,replace=TRUE))
Check out the below examples to understand how it works.
Example
df1<-data.frame(x=sample(seq(from=10,to=50,by=5),size=20,replace=TRUE)) df1
Output
x 1 20 2 15 3 10 4 45 5 45 6 35 7 35 8 50 9 35 10 50 11 10 12 35 13 10 14 25 15 20 16 45 17 50 18 10 19 40 20 10
Example
df2<-data.frame(y=sample(seq(from=0,to=100,by=5),size=20,replace=TRUE)) df2
Output
y 1 85 2 60 3 30 4 65 5 65 6 65 7 35 8 25 9 15 10 70 11 5 12 10 13 65 14 50 15 30 16 75 17 100 18 90 19 15 20 65
Example
df3<-data.frame(z=sample(seq(from=0,to=100,by=10),size=20,replace=TRUE)) df3
Output
z 1 60 2 30 3 80 4 20 5 80 6 100 7 100 8 30 9 10 10 80 11 100 12 10 13 70 14 100 15 20 16 50 17 70 18 30 19 50 20 20
- Related Articles
- How to remove repeated numbers in sequence in R data frame column?
- Find the missing numbers in a sequence in R data frame column.
- How to display numbers with decimal in R data frame column?
- How to add a column in an R data frame with consecutive numbers?
- How to match a column in a data frame with a column in another data frame in R?
- How to replace numbers written in words with numbers in an R data frame column?
- How to generate standard normal random numbers in R?
- How to associate a character to numbers in an R data frame column?
- How to convert a data frame into two column data frame with values and column name as variable in R?
- Python Generate random numbers within a given range and store in a list
- How to replace words in a dichotomous column for an R data frame with numbers?\n
- Python program to generate random numbers within a given range and store in a list?
- Java program to generate random numbers within a given range and store in a list
- How to cut a data frame column with minimum value in R?
- How to multiply vector values in sequence with columns of a data frame in R?

Advertisements