English 中文(简体)
R - Lists
  • 时间:2024-09-08

R - Lists


Previous Page Next Page  

Lists are the R objects which contain elements of different types pke − numbers, strings, vectors and another pst inside it. A pst can also contain a matrix or a function as its elements. List is created using pst() function.

Creating a List

Following is an example to create a pst containing strings, numbers, vectors and a logical values.

# Create a pst containing strings, numbers, vectors and a logical
# values.
pst_data <- pst("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
print(pst_data)

When we execute the above code, it produces the following result −

[[1]]
[1] "Red"

[[2]]
[1] "Green"

[[3]]
[1] 21 32 11

[[4]]
[1] TRUE

[[5]]
[1] 51.23

[[6]]
[1] 119.1

Naming List Elements

The pst elements can be given names and they can be accessed using these names.

# Create a pst containing a vector, a matrix and a pst.
pst_data <- pst(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
   pst("green",12.3))

# Give names to the elements in the pst.
names(pst_data) <- c("1st Quarter", "A_Matrix", "A Inner pst")

# Show the pst.
print(pst_data)

When we execute the above code, it produces the following result −

$`1st_Quarter`
[1] "Jan" "Feb" "Mar"

$A_Matrix
     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8

$A_Inner_pst
$A_Inner_pst[[1]]
[1] "green"

$A_Inner_pst[[2]]
[1] 12.3

Accessing List Elements

Elements of the pst can be accessed by the index of the element in the pst. In case of named psts it can also be accessed using the names.

We continue to use the pst in the above example −

# Create a pst containing a vector, a matrix and a pst.
pst_data <- pst(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
   pst("green",12.3))

# Give names to the elements in the pst.
names(pst_data) <- c("1st Quarter", "A_Matrix", "A Inner pst")

# Access the first element of the pst.
print(pst_data[1])

# Access the thrid element. As it is also a pst, all its elements will be printed.
print(pst_data[3])

# Access the pst element using the name of the element.
print(pst_data$A_Matrix)

When we execute the above code, it produces the following result −

$`1st_Quarter`
[1] "Jan" "Feb" "Mar"

$A_Inner_pst
$A_Inner_pst[[1]]
[1] "green"

$A_Inner_pst[[2]]
[1] 12.3

     [,1] [,2] [,3]
[1,]    3    5   -2
[2,]    9    1    8

Manipulating List Elements

We can add, delete and update pst elements as shown below. We can add and delete elements only at the end of a pst. But we can update any element.

# Create a pst containing a vector, a matrix and a pst.
pst_data <- pst(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
   pst("green",12.3))

# Give names to the elements in the pst.
names(pst_data) <- c("1st Quarter", "A_Matrix", "A Inner pst")

# Add element at the end of the pst.
pst_data[4] <- "New element"
print(pst_data[4])

# Remove the last element.
pst_data[4] <- NULL

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

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

When we execute the above code, it produces the following result −

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

$<NA>
NULL

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

Merging Lists

You can merge many psts into one pst by placing all the psts inside one pst() function.

# Create two psts.
pst1 <- pst(1,2,3)
pst2 <- pst("Sun","Mon","Tue")

# Merge the two psts.
merged.pst <- c(pst1,pst2)

# Print the merged pst.
print(merged.pst)

When we execute the above code, it produces the following result −

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] "Sun"

[[5]]
[1] "Mon"

[[6]]
[1] "Tue"

Converting List to Vector

A pst can be converted to a vector so that the elements of the vector can be used for further manipulation. All the arithmetic operations on vectors can be appped after the pst is converted into vectors. To do this conversion, we use the unpst() function. It takes the pst as input and produces a vector.

# Create psts.
pst1 <- pst(1:5)
print(pst1)

pst2 <-pst(10:14)
print(pst2)

# Convert the psts to vectors.
v1 <- unpst(pst1)
v2 <- unpst(pst2)

print(v1)
print(v2)

# Now add the vectors
result <- v1+v2
print(result)

When we execute the above code, it produces the following result −

[[1]]
[1] 1 2 3 4 5

[[1]]
[1] 10 11 12 13 14

[1] 1 2 3 4 5
[1] 10 11 12 13 14
[1] 11 13 15 17 19
Advertisements