R Data Types | R Datatypes List - r - learn r - r programming



  • The list is a type of vector in R programming.
  • List have different types of element which includes vectors, functions and even another list inside vectors.
  • Each element in the list have different class to represent.
  • A list allows us to fold a variety of (possibly unconnected) objects under one name.
  • Lists are fundamentally different from atomic vectors.
 basic data types r

Syntax:

list()

Sample Code:

x <- list(c(2,4,6),25.01,cos)
print(x)

Code Explanation:

 r datatypes list structure
  1. In this example, x is specifying to string variable. Here we use (list(c(2,4,6),25.01,cos) list function which means to create a list.
  2. Here print() is used to print the value, that value stored in x variable.

Sample Output:

 r datatypes list output
  1. Here in this output we display first list of x variable i.e., (2,4,6) which specifies to numeric values.
  2. In this output we display second list of x variable i.e., (25.01) which specifies to numeric value.
  3. In this output we display third list of x variable i.e., function (x) is string variable and cos is primitive value.

List Elements Manipulation

  • We manipulate the list by add,delete and update elements in a list
  • We can add and delete elements at the end of a list only but we update any elements ina list.

Sample Code

# Create a list containing a vector, a matrix and a list.
list_data <- list(c("Jan","Feb","Mar"), matrix(c(8,4,4,3,-6,9), nrow = 2),
   list("green",12.3))

# Give names to the elements in the list.
names(list_data) <- c("Vectors", "Matrices", "Lists")

# Add element to the list.
list_data[5] <- "New element"
print(list_data[5])

# Remove the element.
list_data[5] <- NULL

# Print the 4th Element.
print(list_data[5])

# Update the 2rd Element.
list_data[2] <- "updated element"
print(list_data[2])

Output

[[1]]
[1] "New element"

$<NA>
NULL

$`A Inner list`
[1] "updated element"


Related Searches to R Data Types | R Datatypes List