100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Lists in r programming $3.79   Add to cart

Class notes

Lists in r programming

 0 view  0 purchase
  • Course
  • Institution

notes for lists in programming explained in details and with examples for better understanding

Preview 2 out of 6  pages

  • May 29, 2023
  • 6
  • 2022/2023
  • Class notes
  • Zahrir
  • All classes
avatar-seller
2/6/23, 8:05 AM R - Lists




R - Lists

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


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

 Live Demo
# Create a list containing strings, numbers, vectors and a logical
# values.
list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
print(list_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 list elements can be given names and they can be accessed using these names.


https://www.tutorialspoint.com/r/r_lists.htm 1/6

, 2/6/23, 8:05 AM R - Lists

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

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


# Show the list.
print(list_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_list
$A_Inner_list[[1]]
[1] "green"


$A_Inner_list[[2]]
[1] 12.3


Accessing List Elements
Elements of the list can be accessed by the index of the element in the list. In case of named lists it
can also be accessed using the names.

We continue to use the list in the above example −

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

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


# Access the first element of the list.
https://www.tutorialspoint.com/r/r_lists.htm 2/6

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller nimairao. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $3.79. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

75759 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$3.79
  • (0)
  Add to cart