![]() |
VOOZH | about |
In this article, we are going to see how to create a list with random values in R programming language.
The list can store data of multiple data type. A List can store multiple R objects like different types of atomic vectors such as character, numeric, logical. We can create a list using list() function. We need to pass vector(s) as parameters.
Syntax : list_variable = list( vector 1,vector 2, . . . . , vector n )
We can generate random values using the sample function:
Syntax : sample( vector , size=value , replace=T or F , prob=c(probability values for the vector) )
Note : Here prob is not mandatory to pass . prob refers the probability of values to be repeated .
Example 1: With replace as FALSE
Output :
[[1]] [1] 7 4 9 1 2 3 10 6 8 5
Explanation :
Example 2: With replace as TRUE
The main point here to remember is if we want to assign replace=FALSE, size is always less than or equal to vector size, otherwise, execution will be halted.
Output :
[[1]] [1] 1 7 8 7 10 2 3 7 8 1 9 7 9 1 1
Example 3: With prob attribute
Output :
[[1]] [1] 3 3 5 5 5 4 4 4 3 5 4 4 3 2 5
Code explanation :