Create a list with random values in R
Last Updated :
02 Sep, 2021
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
R
# creating a list with sample function
lst1 = list(sample(1 : 10, size = 10, replace = F))
# printing the list variable
print(lst1)
Output :
[[1]]
[1] 7 4 9 1 2 3 10 6 8 5
Explanation :
- In the first step, we have created a list using the sample function for generating random values.
- In the sample function, we have taken vectors with 1 to 10 values as the first parameter.
- As the second parameter, we have taken size=10. Since we have taken size=10 it will generate 10 random values from the vector
- As the third parameter, we have taken replace=F. So, no value will be repeated.
- Finally, printing the list variable.
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.
R
# creating a list with sample function
lst1 = list(sample(1 : 10, size = 15, replace = T))
# printing the list variable
print(lst1)
Output :
[[1]]
[1] 1 7 8 7 10 2 3 7 8 1 9 7 9 1 1
Example 3: With prob attribute
R
# creating a list with sample function
lst1 = list(sample(1 : 5, size = 15,
replace = T,
prob = c(0.02, 0.2, 0.25, 0.5, 0.9)))
# printing the list variable
print(lst1)
Output :
[[1]]
[1] 3 3 5 5 5 4 4 4 3 5 4 4 3 2 5
Code explanation :
- In the fourth parameter, we have taken the prob attribute and assigned some probabilities for the vector values. The first value in the prob represents the probability for the first value of the vector. likewise v1=> p1 , v2 => p2. . . . . . vn => pn . Here v represents vector and p represents prob.
- In the output, we can observe that 1 is not there. Because we have given probability as 0.02 . Due to less probability, it is not generated by the r interpreter. Other than 1 all the values are generated and repeated since they have a greater probability as compared to 1.
Similar Reads
Select Random Element from List in R We can select a Random element from a list by using the sample() function. sample() function is used to get the random element from the data structure.Syntax:sample(1:length(list), n)Where:1: length(list) is used to iterate all elements over a list.n represents the number of random elements to be re
1 min read
How to Extract random sample of rows in R DataFrame with nested condition In this article, we will learn how to extract random samples of rows in a DataFrame in R programming language with a nested condition. Method 1: Using sample() We will be using the sample() function to carry out this task. sample() function in R Language creates random samples based on the parameter
4 min read
Randomize Vector in R In this article, we are going to see how to randomize a vector in R Programming Language. Randomize means getting random elements from a vector, we can get the random elements in a vector by using sample() function. Syntax: sample(data, size, replace) Parameters: data: indicates either vector or a p
2 min read
How to randomly shuffle contents of a single column in R dataframe? In this article, we will learn how can we randomly shuffle the contents of a single column using R programming language. Sample dataframe in use: c1c2c3a1w11ab2x22bc3y33cd4z44dMethod1: Using sample() In this approach we have used the transform function to modify our dataframe, then we have passed th
3 min read
Looping Through a List in R Looping through a list is a common operation in R Programming Language allowing you to iterate over elements to perform various tasks, such as applying functions, processing data, or generating output. This article provides an overview of different loop structures in R. Introduction to Lists in RA l
3 min read
How to shuffle a dataframe in R by rows In this article, we will discuss how to shuffle a dataframe by rows in the R programming language. Shuffling means reordering or rearranging the data. We can shuffle the rows in the dataframe by using sample() function. By providing indexing to the dataframe the required task can be easily achieved.
2 min read