4 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Create a Data Frame with Random Numbers in R

How to Create a Data Frame with Random Numbers in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use one of the following methods to create a data frame with random numbers in R:

Method 1: Create Data Frame with Random Values in Range

#create data frame of 10 random values between 1 and 20
df data.frame(matrix(runif(n=10, min=1, max=20), nrow=5))

Method 2: Create Data Frame with Random Integers in Range

#create data frame of 10 random integers between 1 and 20
df data.frame(matrix(round(runif(n=10, min=1, max=20), 0), nrow=5))

The following examples show how to use each of these methods in practice.

Method 1: Create Data Frame with Random Values in Range

The following code shows how to create a data frame with 5 rows consisting of 10 random values between 1 and 20:

#make this example reproducible
set.seed(1)

#create data frame with 10 random numbers between 1 and 20
df data.frame(matrix(runif(n=10, min=1, max=20), nrow=5))

#define column names
names(df) #view data frame
df

          A         B
1  6.044665 18.069404
2  8.070354 18.948830
3 11.884214 13.555158
4 18.255948 12.953167
5  4.831957  2.173939

The result is a data frame with 5 rows and 2 columns, where each value in the data frame is between 1 and 20.

Method 2: Create Data Frame with Random Integers in Range

The following code shows how to create a data frame of 10 random integers between 1 and 50:

#make this example reproducible
set.seed(1)

#create data frame with 10 random integers between 1 and 50
df data.frame(matrix(round(runif(n=10, min=1, max=50), 0), nrow=5))

#define column names
names(df) #view data frame
df

   A  B
1 14 45
2 19 47
3 29 33
4 46 32
5 11  4

The result is a data frame with 5 rows and 2 columns, where each value in the data frame is an integer between 1 and 50.

Note that the runif() function generates random numbers, including the min and max values.

For example, it’s possible that the data frame above could have included both 1 and 50.

Also note that it’s possible for the same number to appear multiple times in the data frame when using this method.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Create a Vector with Random Numbers in R
How to Create a Matrix with Random Numbers in R
How to Select Random Samples in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories