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 <- as.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 <- as.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 <- as.data.frame(matrix(runif(n=10, min=1, max=20), nrow=5)) #define column names names(df) <- c('A', 'B') #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 <- as.data.frame(matrix(round(runif(n=10, min=1, max=50), 0), nrow=5)) #define column names names(df) <- c('A', 'B') #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