Tutorials
r programming
+3

Creating a List in R

Practice Lists in R by using course material from DataCamp's Intro to R course.

If you want to take our free Intro to R course, here is the link.

Creating a list

Let us create our first list! To construct a list you use the function list():

my_list <- list(comp1, comp2 ...)

The arguments to the list function are the list components. Remember, these components can be matrices, vectors, other lists, ...

Instructions

Construct a list, named my_list, that contains the variables my_vector, my_matrix and my_df as list components.

eyJsYW5ndWFnZSI6InIgIiwicHJlX2V4ZXJjaXNlX2NvZGUiOiJcbiIsInNh bXBsZSI6IlxuIyBWZWN0b3Igd2l0aCBudW1lcmljcyBmcm9tIDEgdXAgdG8g MTBcbiAgICAgICAgbXlfdmVjdG9yIDwtIDE6MTBcblxuICAgICAgICAjIE1h dHJpeCB3aXRoIG51bWVyaWNzIGZyb20gMSB1cCB0byA5XG4gICAgICAgIG15 X21hdHJpeCA8LSBtYXRyaXgoMTo5LCBuY29sID0gMylcblxuICAgICAgICAj IEZpcnN0IDEwIGVsZW1lbnRzIG9mIHRoZSBidWlsdC1pbiBkYXRhIGZyYW1l IG10Y2Fyc1xuICAgICAgICBteV9kZiA8LSBtdGNhcnNbMToxMCxdXG5cbiAg ICAgICAgIyBDb25zdHJ1Y3QgbGlzdCB3aXRoIHRoZXNlIGRpZmZlcmVudCBl bGVtZW50czpcbiAgICAgICAgbXlfbGlzdCA8LSIsInNvbHV0aW9uIjoiXG4j IFZlY3RvciB3aXRoIG51bWVyaWNzIGZyb20gMSB1cCB0byAxMFxuICAgICAg ICBteV92ZWN0b3IgPC0gMToxMFxuXG4gICAgICAgICMgTWF0cml4IHdpdGgg bnVtZXJpY3MgZnJvbSAxIHVwIHRvIDlcbiAgICAgICAgbXlfbWF0cml4IDwt IG1hdHJpeCgxOjksIG5jb2wgPSAzKVxuXG4gICAgICAgICMgRmlyc3QgMTAg ZWxlbWVudHMgb2YgdGhlIGJ1aWx0LWluIGRhdGEgZnJhbWUgbXRjYXJzXG4g ICAgICAgIG15X2RmIDwtIG10Y2Fyc1sxOjEwLF1cblxuICAgICA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Gxpc3QoKWAg ZnVuY3Rpb24gd2l0aCBgbXlfdmVjdG9yYCwgYG15X21hdHJpeGAgYW5kIGBt eV9kZmAgYXMgYXJndW1lbnRzIHNlcGFyYXRlZCBieSBhIGNvbW1hLiJ9

Creating a named list

Well done, you're on a roll!

Just like on your to-do list, you want to avoid not knowing or remembering what the components of your list stand for. That is why you should give names to them:

my_list <- list(name1 = your_comp1,
                name2 = your_comp2)

This creates a list with components that are named name1, name2, and so on. If you want to name your lists after you've created them, you can use the names() function as you did with vectors. The following commands are fully equivalent to the assignment above:

my_list <- list(your_comp1, your_comp2)
names(my_list) <- c("name1", "name2")

Instructions

  • Change the code of the previous exercise (see editor) by adding names to the components. Use for my_vector the name vec, for my_matrix the name mat and for my_df the name df.
  • Print out my_list so you can inspect the output.
eyJsYW5ndWFnZSI6InIgIiwicHJlX2V4ZXJjaXNlX2NvZGUiOiJcbiIsInNh bXBsZSI6IlxuIyBWZWN0b3Igd2l0aCBudW1lcmljcyBmcm9tIDEgdXAgdG8g MTBcbiAgICAgICAgbXlfdmVjdG9yIDwtIDE6MTBcblxuICAgICAgICAjIE1h dHJpeCB3aXRoIG51bWVyaWNzIGZyb20gMSB1cCB0byA5XG4gICAgICAgIG15 X21hdHJpeCA8LSBtYXRyaXgoMTo5LCBuY29sID0gMylcblxuICAgICAgICAj IEZpcnN0IDEwIGVsZW1lbnRzIG9mIHRoZSBidWlsdC1pbiBkYXRhIGZyYW1l IG10Y2Fyc1xuICAgICAgICBteV9kZiA8LSBtdGNhcnNbMToxMCxdXG5cbiAg ICAgICAgIyBBZGFwdCBsaXN0KCkgY2FsbCB0byBnaXZlIHRoZSBjb21wb25l bnRzIG5hbWVzXG4gICAgICAgIG15X2xpc3QgPC0gbGlzdChteV92ZWN0b3Is IG15X21hdHJpeCwgbXlfZGYpXG5cbiAgICAgICAgIyBQcmludCBvdXQgbXlf bGlzdCIsInNvbHV0aW9uIjoiXG4jIFZlY3RvciB3aXRoIG51bWVyaWNzIGZy b20g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RoaXM6XG4gICAgICAgICAgICAgICAgICBgYGBcbiAgICAg ICAgICAgICAgICAgIG15X2xpc3QgPC0gbGlzdCh2ZWMgPSBteV92ZWN0b3Ip XG4gICAgICAgICAgICAgICAgICBgYGBcbiAgICAgICAgICAgICAgQWRkIHRo ZSBvdGhlciB0d28gY29tcG9uZW50cyBpbiBhIHNpbWlsYXIgZmFzaGlvbi4i fQ==

Creating a named list (2)

Being a huge movie fan (remember your job at LucasFilms), you decide to start storing information on good movies with the help of lists.

Start by creating a list for the movie "The Shining". We have already created the variables mov, act and rev in your R workspace. Feel free to check them out in the console.

Instructions

Complete the code on the right to create shining_list; it contains three elements:

  • moviename: a character string with the movie title (stored in mov)
  • actors: a vector with the main actors' names (stored in act)
  • reviews: a data frame that contains some reviews (stored in rev)

Do not forget to name the list components accordingly (names are moviename, actors and reviews).

eyJsYW5ndWFnZSI6InIgIiwicHJlX2V4ZXJjaXNlX2NvZGUiOiJcbiAgbW92 IDwtIFwiVGhlIFNoaW5pbmdcIlxuICAgICAgICBhY3QgICAgICA8LSBjKFwi SmFjayBOaWNob2xzb25cIixcIlNoZWxsZXkgRHV2YWxsXCIsXCJEYW5ueSBM bG95ZFwiLFwiU2NhdG1hbiBDcm90aGVyc1wiLFwiQmFycnkgTmVsc29uXCIp XG4gICAgICAgIHNvdXJjZXMgICAgIDwtIGMoXCJJTURiMVwiLFwiSU1EYjJc IixcIklNRGIzXCIpXG4gICAgICAgIGNvbW1lbnRzICAgIDwtIGMoXCJCZXN0 IEhvcnJvciBGaWxtIEkgSGF2ZSBFdmVyIFNlZW5cIixcIkEgdHJ1bHkgYnJp bGxpYW50IGFuZCBzY2FyeSBmaWxtIGZyb20gU3RhbmxleSBLdWJyaWNrXCIs XCJBIG1hc3RlcnBpZWNlIG9mIHBzeWNob2xvZ2ljYWwgaG9ycm9yXCIpXG4g ICAgICAgIHNjb3JlcyAgICAgIDwtIGMoNC41LDQsNSlcbiAgICAgICAgcmV2 ICAgICA8LSBkYXRh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AsIGFuZCB0aGUgdGhpcmQgYHJldmAuXCIpXG4g ICAgICAgIHRlc3Rfb2JqZWN0KFwic2hpbmluZ19saXN0XCIsIGVxX2NvbmRp dGlvbiA9IFwiZXF1YWxcIixcbiAgICAgICAgICAgICAgICAgICAgaW5jb3Jy ZWN0X21zZyA9IFwiSXQgbG9va3MgbGlrZSBgc2hpbmluZ19saXN0YCBkb2Vz IG5vdCBjb250YWluIHRoZSBjb3JyZWN0IG5hbWluZyBmb3IgdGhlIGNvbXBv bmVudHM6IG5hbWUgdGhlIGZpcnN0IGVsZW1lbnQgYG1vdmllbmFtZWAsIHRo ZSBzZWNvbmQgZWxlbWVudCBgYWN0b3JzYCwgYW5kIHRoZSB0aGlyZCBlbGVt ZW50IGByZXZpZXdzYC5cIik7XG4gICAgICAgIHN1Y2Nlc3NfbXNnKFwiV29u ZGVyZnVsISBZb3Ugbm93IGtub3cgaG93IHRvIGNvbnN0cnVjdCBhbmQgbmFt ZSBsaXN0cy4gQXMgaW4gdGhlIHByZXZpb3VzIGNoYXB0ZXJzLCBsZXQncyBs b29rIGF0IGhvdyB0byBzZWxlY3QgZWxlbWVudHMgZm9yIGxpc3RzLiBIZWFk IG92ZXIgdG8gdGhlIG5leHQgZXhlcmNpc2VcIikiLCJoaW50IjoiXG5gYHNo aW5pbmdfbGlzdCA8LSBsaXN0KG1vdmllbmFtZSA9IG1vdilgYCBpcyBvbmx5 IHBhcnQgb2YgdGhlIHNvbHV0aW9uOyBpdCdzIHlvdXIgam9iIHRvIGFsc28g YWRkIGBhY3RgIGFuZCBgcmV2YCB0byB0aGUgbGlzdCwgd2l0aCB0aGUgYXBw cm9wcmlhdGUgbmFtZXMuIn0=


If you want to learn more from this course, here is the link.

Want to leave a comment?