Select Random Element from List in R Last Updated : 19 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 returned.Note: In our case n=1 because we have to get only one random element.Example 1: R program to create a list of numbers and return one random element R list1=list(1:10,23,45,67,8) # get the random element from the list list1[[sample(1:length(list1), 1)]] Output:Test 1:[1] 67Test 2:[1] 45Example 2: R program to create a list of numbers and return one random elementWe create a numeric vector containing the elements 1 to 5. Then, we define a character string data with the value "Hello Geek." These two objects are combined into a list. The line list1[[sample(1:length(list1), 1)]] uses the sample() function to randomly select an index (either 1 or 2) from the list R vec=c(1,2,3,4,5) data="Hello Geek" list1=list(vec,data) list1[[sample(1:length(list1), 1)]] Output:Test 1:[1] 1 2 3 4 5Test 2:Hello Geek Comment More infoAdvertise with us Next Article Select Random Element from List in R sravankumar_171fa07058 Follow Improve Article Tags : R Language R Programs R-List R List-Programs Similar Reads How to Remove Specific Elements from Vector in R? In this article, we will discuss how to remove specific elements from vectors in R Programming Language. Remove elements using in operatorThis operator will select specific elements and uses ! operator to exclude those elements. Syntax: vector[! vector %in% c(elements)]In this example, we will be us 2 min read Extract column from list in R In this article, we are going to create a list of elements and access those columns in R. We are first creating a list with matrix and vectors and access those columns using R. Approach Create a list Syntax: list_name=list(var1, var2, varn..) Assign names to list elements as columns names. We can gi 3 min read Select Only Numeric Columns from DataFrame in R In this article, we will discuss how to select only numeric columns from dataframe in R Programming Language. Method 1: Using Dplyr package We can use select_if() function to get numeric columns by calling the function with the dataframe name and isnumeric() function that will check for numeric colu 2 min read Get element at the specific position from matrix in R At any point in time, a matrix may be required to be traversed for an element at a specific position. In this article, we are going to access the elements from a matrix in R Programming Language using integer vector, and logical vector as the index. Accessing Elements in a MatrixIn R Programming Lan 2 min read Convert List of Vectors to DataFrame in R In this article, we will discuss how to convert the given list of vectors to Dataframe using the help of different functions in the R Programming Language. For all the methods discussed below, few functions are common because of their functionality. Let us discuss them first. as.data.frame() is one 2 min read Select DataFrame Rows where Column Values are in Range in R In this article, we will discuss how to select dataframe rows where column values are in a range in R programming language. Data frame indexing can be used to extract rows or columns from the dataframe. The condition can be applied to the specific columns of the dataframe and combined using the logi 2 min read Convert dataframe to list of vectors in R In this article, we will learn how to convert a dataframe into a list of vectors, such that we can use columns of the dataframes as vectors in the R Programming language. Dataframe columns as a list of vectors as.list() function in R Language is used to convert an object to a list. These objects can 3 min read Count Number of List Elements in R In this article, we will explore how to count the number of elements in a list in R, including both simple and nested lists. We'll use two key functions:length() to count the number of top-level elements in a list.lengths() to count the number of elements within each top-level list component.These f 2 min read Convert dataframe column to list in R In this article, we will learn how to convert a dataframe into a list by columns in R Programming language. We will be using as.list() function, this function is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and data frames. Syntax: as.list( object ) Parameter 2 min read Select Odd and Even Rows and Columns from DataFrame in R In this article, we will discuss how to select odd and even rows from a dataframe in R programming language. Getting Odd Rows from the Data Frame The number of rows in a data frame in R can be fetched by the nrow() method. It returns the number of rows in the data frame. The seq_len() method is then 6 min read Like