How can I create a data frame containing random numbers in R?

How can I create a data frame containing random numbers in R?

A data frame is a fundamental data structure in the R programming language that allows for the storage and manipulation of data. To create a data frame containing random numbers in R, the user can utilize the built-in function “data.frame()” and the “runif()” function from the “stats” package. The “runif()” function generates a specified number of random numbers within a given range, which can then be used as the values for each column in the data frame. The resulting data frame will contain the specified number of rows and columns, with each column containing random numbers within the specified range. This allows for the creation of a data frame that can be used for various statistical analysis and data visualization tasks.

Create a Data Frame with Random Numbers in R


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.

Cite this article

stats writer (2024). How can I create a data frame containing random numbers in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-data-frame-containing-random-numbers-in-r/

stats writer. "How can I create a data frame containing random numbers in R?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-data-frame-containing-random-numbers-in-r/.

stats writer. "How can I create a data frame containing random numbers in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-data-frame-containing-random-numbers-in-r/.

stats writer (2024) 'How can I create a data frame containing random numbers in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-data-frame-containing-random-numbers-in-r/.

[1] stats writer, "How can I create a data frame containing random numbers in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create a data frame containing random numbers in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top