The Wayback Machine - https://web.archive.org/web/20240828110752/https://www.geeksforgeeks.org/convert-an-object-to-list-in-r-programming-as-list-function/
Open In App

Convert an Object to List in R Programming – as.list() Function

Last Updated : 27 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

as.list() function in R Programming Language is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and dataframes.

Syntax: as.list(object)

Parameters: 

object: Vector, Matrix, factor, or data frame 

R – as.list() Function Example

Example 1: Converting Vector to list using as.list() function in R Language

R




# R program to convert object to list
 
# Creating vector
x < - c(1, 2, 3, 4, 5)
 
# Calling as.list() Function
as.list(x)


Output: 

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

Example 2: Converting data frame to list using as.list() function in R Language

R




# R program to convert object to list
 
# Calling pre-defined data set
BOD
 
# Calling as.list() Function
as.list(BOD)


Output: 

  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8
$Time
[1] 1 2 3 4 5 7

$demand
[1]  8.3 10.3 19.0 16.0 15.6 19.8

attr(, "reference")
[1] "A1.4, p. 270"

Example 3: Converting matrix to list using as.list() function in R Language

R




# R program to create a matrix
# and then convert into list
 
A = matrix(
     
# Taking sequence of elements
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
 
# No of rows
nrow = 3,
 
# No of columns
ncol = 3,       
 
# By default matrices are in column-wise order
# So this parameter decides how to arrange the matrix
byrow = TRUE       
)
 
# Naming rows
rownames(A) = c("a", "b", "c")
 
# Naming columns
colnames(A) = c("c", "d", "e")
 
cat("The 3x3 matrix:\n")
print(A)
 
 
lst = as.list(A)
lst


Output:

The 3x3 matrix:
  c d e
a 1 2 3
b 4 5 6
c 7 8 9
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 7

[[4]]
[1] 2

[[5]]
[1] 5

[[6]]
[1] 8

[[7]]
[1] 3

[[8]]
[1] 6

[[9]]
[1] 9
 


Similar Reads

Convert type of data object in R Programming - type.convert() Function
type.convert() function in R Language is used to compute the data type of a particular data object. It can convert data object to logical, integer, numeric, or factor. Syntax: type.convert(x) Parameter: x: It can be a vector matrix or an array Example 1: Apply type.convert to vector of numbers # R program to illustrate # type.convert to vector of n
2 min read
Convert an Object to Data Frame in R Programming - as.data.frame() Function
as.data.frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors. Syntax: as.data.frame(object) Parameters: object: Vector, Matrix, factor, or data frameR - as.data.frame() Function ExampleExample 1: Basic example of as.data.frame() Function in R C/C++ Code # R
2 min read
Convert a Numeric Object to Character in R Programming - as.character() Function
as.character() function in R Language is used to convert a numeric object to character object. Syntax: as.character(x) Parameters: x: Numeric Object Example 1: # R program to convert a numeric object # to character object # Calling as.character() function as.character(1) as.character(2 + 3) as.character(1.5) Output: [1] "1" [1] "5" [1] "1.5" Exampl
1 min read
Convert a Character Object to Integer in R Programming - as.integer() Function
as.integer() function in R Language is used to convert a character object to integer object. Syntax: as.integer(x) Parameters: x: Character Object Example 1: # R program to convert a character object # to an integer object # Calling as.integer() function as.integer(&quot;4&quot;) as.integer(&quot;1.6&quot;) as.integer(&quot;-3.2&quot;) as.integer(
1 min read
Convert values of an Object to Logical Vector in R Programming - as.logical() Function
as.logical() function in R Language is used to convert an object to a logical vector. Syntax: as.logical(x)Parameters: x: Numeric or character object R - as.logical() Function ExampleExample 1: Basic example of as.logical() Function in R Programming Language. C/C++ Code # R Program to convert # an object to logical vector # Creating a vector x &lt;
1 min read
Convert an Object into a Vector in R Programming - as.vector() Function
as.vector() function in R Language is used to convert an object into a vector. Syntax: as.vector(x) Parameters: x: Object to be converted Example 1: # R program to convert an object to vector # Creating an array x &lt;- array(c(2, 3, 4, 7, 2, 5), c(3, 2)) x # Calling as.vector() Function as.vector(x) Output: [, 1] [, 2] [1, ] 2 7 [2, ] 3 2 [3, ] 4
1 min read
Convert an Object to a String in R Programming - toString() Function
toString() function in R Language is used to convert an object into a single character string. Syntax: toString(x, width) Parameters: x: Object width: maximum string width Example 1: # R program to convert an object to string # Creating a vector x &lt;- c(&quot;Geeks&quot;, &quot;for&quot;, &quot;geeks&quot;) # Calling toString() Function toString(
1 min read
Convert an Object to a Table in R Programming - as.table() Function
as.table() function in R Language is used to convert an object into a table. Syntax: as.table(x) Parameters: x: Object to be converted Example 1: # R Program to convert # an object to a table # Creating a vector vec = c(2, 4, 3, 1, 2, 3, 2, 1, 4, 2) # Calling as.table() Function as.table(vec) Output: A B C D E F G H I J 2 4 3 1 2 3 2 1 4 2 Example
1 min read
Convert an Object into a Matrix in R Programming - as.matrix() Function
In R Programming Language as. matrix() function is used to convert an object into a Matrix. Syntax: as.matrix(x) Parameters: x: Object to be converted as.matrix() Function in R ExampleExample 1: Convert vector to the matrix using as.matrix() C/C++ Code # R program to convert an object to matrix # Creating a vector x &lt;- c(1:9) # Calling as.matrix
2 min read
Check if the Object is a List in R Programming - is.list() Function
is.list() function in R Language is used to return TRUE if the specified data is in the form of list, else returns FALSE. Syntax: is.list(X) Parameters: x: different types of data storage Example 1: # R program to illustrate # is.list function # Initializing some list a &lt;- list(1, 2, 3) b &lt;- list(c(&quot;Jan&quot;, &quot;Feb&quot;, &quot;Mar
1 min read
Applying a Function over an Object in R Programming - sapply() Function
sapply() function in R Language takes list, vector or data frame as input and gives output in vector or matrix. It is useful for operations on list objects and returns a list object of same length of original set. Syntax: sapply(X, FUN) Parameters: X: A vector or an object FUN: Function applied to each element of x Example 1: # R program to illustr
1 min read
Coercing an Object of mode "list" to mode "call" in R Programming - as.call() Function
as.call() function in R Language is used to coerce the object of mode "list" to mode "call". The first element of the list becomes the function part of the call. Syntax: as.call(x) Parameters: x: an arbitrary R object Example 1: # R program to illustrate # as.call function # Calling the as.call() function as.call(list(list, 5, 10)) as.call(list(as.
1 min read
Print the Value of an Object in R Programming - identity() Function
identity() function in R Language is used to print the value of the object which is passed to it as argument. Syntax: identity(x) Parameters: x: An Object Example 1: # R program to print # the value of an object # Creating a vector x &lt;- c(1, 2, 3, 4) # Creating a list list1 &lt;- list(letters[1:6]) # Calling the identity function identity(x) ide
1 min read
Return True Indices of a Logical Object in R Programming - which() Function
which() function in R Language is used to return the indices of the object which return true for the logical operation passed as argument. Syntax: which(x, arr.ind) Parameters: x: logical object arr.ind: Boolean value to display indices Example 1: # R program to illustrate # the use of which() function # Create a matrix x &lt;- matrix(1:9, 3, 3) x
1 min read
Create an Object of mode call in R Programming - call() Function
call() function in R Language is used to create or test for objects of mode "call". Syntax: call(name, ...) Parameters: name: a non-empty character string naming the function to be called ...: arguments to be part of the call Example 1: # R program to illustrate # call function # Calling call() function x &lt;- call(&quot;sin&quot;, 23) y &lt;- cal
1 min read
Return an Object with the specified name in R Programming - get0() and mget() Function
In R programming, get0() and mget() function works similar to get() function. It is used to search and return the object with the specified name passed to it as argument. get0() Function get0() function has the same syntax as get() function but there is an addition of a new parameter which returns an output if the data object is not found. Clearly,
2 min read
Calculate Cumulative Product of a Numeric Object in R Programming – cumprod() Function
cumprod() function in R Language is used to calculate the cumulative product of the vector passed as argument. Syntax: cumprod(x) Parameters: x: Numeric Object Example 1: # R program to illustrate # the use of cumprod() Function # Calling cumprod() Function cumprod(1:4) cumprod(-1:-6) Output: [1] 1 2 6 24 [1] -1 2 -6 24 -120 720 Example 2: # R prog
1 min read
Check if Object is of the Character Data type in R Programming - is.character() Function
is.character() Function in R Language is used to check if the object is of the form of a string/character or not. It will return true if any element of the object is of the character data type. Syntax: is.character(Object) Parameter: Object: It could be an array, list, vector, etc. Example 1: # R Program to illustrate # the use of is.character func
1 min read
Get the number of columns of an Object in R Programming - ncol() Function
ncol() function in R Language is used to return the number of columns of the specified matrix. Syntax: ncol(x)Parameters: x: matrix, vector, array or data frame Example 1: C/C++ Code # R program to illustrate # ncol function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling ncol() function to # get the number of columns ncol(BOD) Output:
1 min read
Checking if the Object is a Factor in R Programming - is.factor() Function
is.factor() function in R Language is used to check if the object passed to the function is a Factor or not. It returns a boolean value as output. Syntax: is.factor(Object) Parameters: Object: Object to be checked Example 1: # Creating a vector x&lt;-c(&quot;female&quot;, &quot;male&quot;, &quot;male&quot;, &quot;female&quot;) # Converting the vect
1 min read
Get the number of rows of an Object in R Programming - nrow() Function
nrow() function in R Language is used to return the number of rows of the specified matrix. Syntax: nrow(x)Parameters: x: matrix, vector, array or data frame Example 1: C/C++ Code # R program to illustrate # nrow function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling nrow() function to # get the number of rows nrow(BOD) Output: Time d
1 min read
Remove Duplicate Elements from an Object in R Programming - unique() Function
unique() function in R Language is used to remove duplicated elements/rows from a vector, data frame or array. Syntax: unique(x) Parameters: x: vector, data frame, array or NULL Example 1: # R program to illustrate # unique function # Initializing some set of numbers x &lt;- c(1:10, 5:9) x # Calling unique() function to print # unique set of number
1 min read
Get the Maximum element of an Object in R Programming - max() Function
max() function in R Language is used to find the maximum element present in an object. This object can be a Vector, a list, a matrix, a data frame, etc.. Syntax: max(object, na.rm) Parameters: object: Vector, matrix, list, data frame, etc. na.rm: Boolean value to remove NA element. Example 1: # R program to illustrate # the use of max() function #
1 min read
Get the Minimum element of an Object in R Programming - min() Function
min() function in R Language is used to find the minimum element present in an object. This object can be a Vector, a list, a matrix, a data frame, etc.. Syntax: min(object, na.rm) Parameters: object: Vector, matrix, list, data frame, etc. na.rm: Boolean value to remove NA element. Example 1: # R program to illustrate # the use of min() function #
1 min read
Display the internal Structure of an Object in R Programming - str() Function
str() function in R Language is used for compactly displaying the internal structure of a R object. It can display even the internal structure of large lists which are nested. It provides one liner output for the basic R objects letting the user know about the object and its constituents. It can be used as an alternative to summary() but str() is m
3 min read
Calculate Cumulative Sum of a Numeric Object in R Programming - cumsum() Function
The cumulative sum can be defined as the sum of a set of numbers as the sum value grows with the sequence of numbers. cumsum() function in R Language is used to calculate the cumulative sum of the vector passed as argument. Syntax: cumsum(x) Parameters: x: Numeric Object Example 1: C/C++ Code # R program to illustrate # the use of cumsum() Function
1 min read
Search and Return an Object with the specified name in R Programming - get() Function
get() function in R Language is used to return an object with the name specified as argument to the function. This is another method of printing values of the objects just like print function. This function can also be used to copy one object to another. Syntax: get(object) Parameters: object: Vector, array, list, etc. Example 1: # R program to ill
1 min read
Sort elements of an Object by its Index Values in R Programming - order() function
order() function in R Language is used to sort an object by its index value. These objects can be vector, matrix or data frames. This function accepts boolean value as argument to sort in ascending or descending order. Syntax: order(x, decreasing, na.last) Parameters: x: Vector to be sorted decreasing: Boolean value to sort in descending order na.l
1 min read
Calculate the Mean of each Row of an Object in R Programming – rowMeans() Function
rowMeans() function in R Language is used to find out the mean of each row of a data frame, matrix, or array. Syntax: rowMeans(data) Parameter: data: data frame, array, or matrix Example 1 # R program to illustrate # rowMean function # Create example values # Set seed set.seed(1234) # Create example data data &lt;- data.frame(matrix(round(runif(12,
1 min read
Get or Set names of Elements of an Object in R Programming - names() Function
names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object. The length of the value vector passed must be exactly equal to the length of the object to be named. Syntax: names(x) &lt;- value Para
2 min read
Article Tags :