Skip to content

Instantly share code, notes, and snippets.

@abhishekdagarit
Last active October 30, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhishekdagarit/5839abc72c5cb1d263dc5d7ba3a68021 to your computer and use it in GitHub Desktop.
Save abhishekdagarit/5839abc72c5cb1d263dc5d7ba3a68021 to your computer and use it in GitHub Desktop.
Lists in R

Creating a list

A list is like a super data type that can hold any other data type inside it.

# Vector with numerics from 12 up to 100
my_vector <- 12:100 

# Matrix with numerics from 1 up to 8
my_matrix <- matrix(1:8, ncol = 2)

# First 13 elements of the built-in data frame mtcars
my_df <- mtcars[1:13,]

# Construct list with these different elements:
my_list <- list(my_vector, my_matrix, my_df)

Naming lists

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

The above code can be understood from:

shining_list <- list(moviename = movie, actors = actor, reviews = review)

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)

my_list <- list(my_vector, my_matrix, my_df)
names(my_list) <- c("vec","mat","df")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment