Just to address the last part of your question, since that really points out the difference between a list and vector in R:

Why do these two expressions not return the same result?

x = list(1, 2, 3, 4); x2 = list(1:4)

A list can contain any other class as each element. So you can have a list where the first element is a character vector, the second is a data frame, etc. In this case, you have created two different lists. x has four vectors, each of length 1. x2 has 1 vector of length 4:

> length(x[[1]])
[1] 1
> length(x2[[1]])
[1] 4

So these are completely different lists.

R lists are very much like a hash map data structure in that each index value can be associated with any object. Here's a simple example of a list that contains 3 different classes (including a function):

> complicated.list <- list("a"=1:4, "b"=1:3, "c"=matrix(1:4, nrow=2), "d"=search)
> lapply(complicated.list, class)
$a
[1] "integer"
$b
[1] "integer"
$c
[1] "matrix"
$d
[1] "function"

Given that the last element is the search function, I can call it like so:

> complicated.list[["d"]]()
[1] ".GlobalEnv" ...

As a final comment on this: it should be noted that a data.frame is really a list (from the data.frame documentation):

A data frame is a list of variables of the same number of rows with unique row names, given class ‘"data.frame"’

That's why columns in a data.frame can have different data types, while columns in a matrix cannot. As an example, here I try to create a matrix with numbers and characters:

> a <- 1:4
> class(a)
[1] "integer"
> b <- c("a","b","c","d")
> d <- cbind(a, b)
> d
 a   b  
[1,] "1" "a"
[2,] "2" "b"
[3,] "3" "c"
[4,] "4" "d"
> class(d[,1])
[1] "character"

Note how I cannot change the data type in the first column to numeric because the second column has characters:

> d[,1] <- as.numeric(d[,1])
> class(d[,1])
[1] "character"
Answer from Shane on Stack Overflow
🌐
Datamentor
datamentor.io › r-programming › list
R Lists (With Examples)
x <- list(name = "John", age = 19, speaks = c("English", "French")) # access element by exact matching using $ x$name # access element by partial matching using $ x$age # access element by partial matching using $ x$speaks # create a list with similar tags y <- list(n = "Alice", a = 25, s = c("Spanish", "Italian")) # access element by partial matching using $ y$n # access element by partial matching using $ y$a # access element by partial matching using $ y$s ... We can change components of a list through reassignment.
🌐
R-bloggers
r-bloggers.com › r bloggers › the ultimate guide to creating lists in r: from basics to advanced examples
The Ultimate Guide to Creating Lists in R: From Basics to Advanced Examples | R-bloggers
October 29, 2024 - List of 3 $ numbers: int [1:5] 1 2 3 4 5 $ text : chr "Hello" $ nested :List of 2 ..$ a: num 1 ..$ b: num 2 · Try creating a list with the following specifications: - Create a list named car_info - Include make (character), year (numeric), and features (character vector) - Add a price element after creation
Discussions

r - How to correctly use lists? - Stack Overflow
Bring the best of human thought ... at your work. Explore Stack Internal ... Brief background: Many (most?) contemporary programming languages in widespread use have at least a handful of ADTs [abstract data types] in common, in particular, ... In the R programming language, the first two are implemented as character and vector, respectively. When I began learning R, two things were obvious almost from the start: list is the most ... More on stackoverflow.com
🌐 stackoverflow.com
Working with Large JSON file in List
but when I plug in the actual JSON data (118k lines of data, 2.2mb) then the SearchView does not display the items at all How are you converting JSON data into your model objects? What do your model objects look like? You should post your code otherwise we can only guess what’s gone wrong. it is just bad practice to read from huge JSON files like this? Usually, yes. The benefit of using SQLite or Core Data is that you don’t have to read the entire file into memory each time you want to access some of it. The trade off is they’re a lot more complicated to use than JSON. would I be able to still load all of this data into the list at once You should avoid doing this, especially with larger datasets. The user will never see more than a fraction of those items on their screen at a single time so it’s common to use batching, which is where you fetch the items in batches as they’re needed. For example you might load the first 50 items then if the user scrolls you fetch the next 50 and so on. This is how apps like Photos can load instantly despite containing hundreds of thousands of images and videos. More on reddit.com
🌐 r/swift
3
1
October 27, 2022
List of Systems Working with GarlicOS
Any chance you could mention what bios file names and their relative locations on the system. I have a whole bunch of bios files in BIOS, but it seems like half of them aren't working. Not sure if they need to be in core subdirs or what. More on reddit.com
🌐 r/RG35XX
26
35
March 8, 2023
Reddit - The heart of the internet
Reddit is where millions of people gather for conversations about the things they care about, in over 100,000 subreddit communities. More on reddit.com
🌐 reddit.com
4 days ago
🌐
Bioconnector
bioconnector.github.io › workshops › r-lists.html
List Manipulation
Now that we’ve figured out how to calculate the values we’re interested in, we just need to append them to the original list. One of the keys here is appreciating that lapply() can take any function (including one that we write … an “anonymous function”3) and use that operation on each element in the list. Another point worth noting is that the c() function works on lists.
🌐
DataFlair
data-flair.training › blogs › r-list-tutorial
R List - Learn what all you can do with Lists in R! - DataFlair
May 8, 2024 - In this R List tutorial, learn about R list, its creation, naming, accessing and manipulating list elements in R.
🌐
R-bloggers
r-bloggers.com › r bloggers › how to use lists in r
How to use lists in R | R-bloggers
August 30, 2015 - Error ## out1 2.7707490 0.1915748 ## out2 4.7340543 0.1876549 ## out3 -0.1344969 0.1912755 ## out4 1.3293520 0.5324664 #add outcome columnn and change name of SE column coefs$Outcome<-rownames(coefs) names(coefs)[2]<-"SE" #use ggplot to plot all the estimates require(ggplot2) ggplot(coefs, aes(Outcome,Estimate)) + geom_point(size=4) + theme(legend.position="none")+ labs(title="Treatment effect on outcomes", x="", y="Estimate and 95% CI")+ geom_errorbar(aes(ymin=Estimate-1.96*SE,ymax=Estimate+1.96*SE),width=0.1)+ geom_hline(yintercept = 0, color="red")+ coord_flip() I hope that was useful! There are many great ways to use lists and the apply() functions to make your programming more efficient and less prone to errors. For another great resource on using the apply() functions with lists, definitely check out this StackOverflow page.
🌐
DataCamp
datacamp.com › tutorial › creating-lists-r
R List: Create a List in R with list() | DataCamp
September 27, 2018 - 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.
🌐
R Tutor
r-tutor.com › r-introduction › list
List | R Tutorial
> n = c(2, 3, 5) > s = c("aa", "bb", "cc", "dd", "ee") > b = c(TRUE, FALSE, TRUE, FALSE, FALSE) > x = list(n, s, b, 3) # x contains copies of n, s, b
Find elsewhere
🌐
W3Schools
w3schools.com › r › r_lists.asp
R Lists
Add "orange" to the list after "banana" (index 2): thislist <- list("apple", "banana", "cherry") append(thislist, "orange", after = 2) Try it Yourself » · You can also remove list items. The following example creates a new, updated list without an "apple" item:
Top answer
1 of 13
162

Just to address the last part of your question, since that really points out the difference between a list and vector in R:

Why do these two expressions not return the same result?

x = list(1, 2, 3, 4); x2 = list(1:4)

A list can contain any other class as each element. So you can have a list where the first element is a character vector, the second is a data frame, etc. In this case, you have created two different lists. x has four vectors, each of length 1. x2 has 1 vector of length 4:

> length(x[[1]])
[1] 1
> length(x2[[1]])
[1] 4

So these are completely different lists.

R lists are very much like a hash map data structure in that each index value can be associated with any object. Here's a simple example of a list that contains 3 different classes (including a function):

> complicated.list <- list("a"=1:4, "b"=1:3, "c"=matrix(1:4, nrow=2), "d"=search)
> lapply(complicated.list, class)
$a
[1] "integer"
$b
[1] "integer"
$c
[1] "matrix"
$d
[1] "function"

Given that the last element is the search function, I can call it like so:

> complicated.list[["d"]]()
[1] ".GlobalEnv" ...

As a final comment on this: it should be noted that a data.frame is really a list (from the data.frame documentation):

A data frame is a list of variables of the same number of rows with unique row names, given class ‘"data.frame"’

That's why columns in a data.frame can have different data types, while columns in a matrix cannot. As an example, here I try to create a matrix with numbers and characters:

> a <- 1:4
> class(a)
[1] "integer"
> b <- c("a","b","c","d")
> d <- cbind(a, b)
> d
 a   b  
[1,] "1" "a"
[2,] "2" "b"
[3,] "3" "c"
[4,] "4" "d"
> class(d[,1])
[1] "character"

Note how I cannot change the data type in the first column to numeric because the second column has characters:

> d[,1] <- as.numeric(d[,1])
> class(d[,1])
[1] "character"
2 of 13
67

Regarding your questions, let me address them in order and give some examples:

1) A list is returned if and when the return statement adds one. Consider

 R> retList <- function() return(list(1,2,3,4)); class(retList())
 [1] "list"
 R> notList <- function() return(c(1,2,3,4)); class(notList())
 [1] "numeric"
 R> 

2) Names are simply not set:

R> retList <- function() return(list(1,2,3,4)); names(retList())
NULL
R> 

3) They do not return the same thing. Your example gives

R> x <- list(1,2,3,4)
R> x[1]
[[1]]
[1] 1
R> x[[1]]
[1] 1

where x[1] returns the first element of x -- which is the same as x. Every scalar is a vector of length one. On the other hand x[[1]] returns the first element of the list.

4) Lastly, the two are different between they create, respectively, a list containing four scalars and a list with a single element (that happens to be a vector of four elements).

🌐
Steve's Data Tips and Tricks
spsanderson.com › steveondata › posts › 2024-10-29
The Ultimate Guide to Creating Lists in R: From Basics to Advanced Examples – Steve’s Data Tips and Tricks
October 29, 2024 - Try creating a list with the following specifications: - Create a list named car_info - Include make (character), year (numeric), and features (character vector) - Add a price element after creation ... # Create the initial list car_info <- list( make = "Toyota", year = 2024, features = c("GPS", "Bluetooth", "Backup Camera") ) # Add price element car_info$price <- 25000 # Print the result print(car_info)
🌐
Discdown
discdown.org › rprogramming › lists.html
Chapter 9 Lists | Introduction to Programming with R
Delete elements: To delete an element, we simply have to replace it with a NULL object. An example using a very simple list: ... As you can see it is possible that a list element can contain NULL (see element c) but if assigned (x$a <- NULL) R will remove the element completely (not storing NULL on it). Exercise 9.2 Practicing replacement: As in the previous exercise we will use the following list to work with.
🌐
TutorialsPoint
tutorialspoint.com › r › r_lists.htm
R - Lists
[[1]] [1] "Red" [[2]] [1] "Green" [[3]] [1] 21 32 11 [[4]] [1] TRUE [[5]] [1] 51.23 [[6]] [1] 119.1 · The list elements can be given names and they can be accessed using these names.
🌐
TechVidvan
techvidvan.com › tutorials › r-list
R List - How to create, index and manipulate list components - TechVidvan
January 4, 2020 - We can modify a component by accessing them through indexing and reassigning the new value. ... List of 5 $ name : chr “harry” $ wife name : chr “jenny” $ age : num 42 $ children : num 2 $ children age: num [1:2] 14 17 · We can add a new component by assigning it to an unused index.
🌐
Rfaqs
rfaqs.com › data-structure › list
List - R Programming FAQs
From the above examples, the following are key points that need to be remembered when dealing with lists in R Programming: Use NULL Assignment for deleting elements: list$element <- NULL · Negative indexing works, but renumbers remaining elements
🌐
GeeksforGeeks
geeksforgeeks.org › operations-on-lists-in-r-programming
Operations on Lists in R Programming | GeeksforGeeks
August 25, 2020 - A list in R is basically an R object ... a list can contain other objects which may be of varying lengths. The list is defined using the list() function in R....
🌐
UC Business Analytics
uc-r.github.io › lists
Managing Lists · UC Business Analytics R Programming Guide
Answer the following questions about this list: ... Extract the coefficients of this model. Extract the departure delay (dep_delay) coefficient. Its important to understand the difference between simplifying and preserving subsetting. Simplifying subsets returns the simplest possible data structure that can represent the output. Preserving subsets keeps the structure of the output the same as the input.
🌐
YouTube
youtube.com › watch
Working with lists in R - YouTube
Lists are a super flexible data structure. I use them regularly to organize related data sets in one place. Let's go!If this vid helps you, please help me a ...
Published   August 10, 2022
🌐
NPS
faculty.nps.edu › sebuttre › home › R › lists.html
Lists in R
mylist <- list (a = 1:5, b = "Hi There", c = function(x) x * sin(x)) Now the list "mylist" contains three things, named "a," "b," and "c." Their lengths are different: a has length 5, b has length 1, and c is a function, so it doesn't really have a length. (Technically, it has length 1, just because somebody decided that the "length" of a function should be one.) To extract an item from a list, you can use the single brackets, but that will give you back a list. Thus · mylist[1] $a: [1] 1 2 3 4 5 > mylist[1] + 1 # Can we do math on that? Error in mylist[1] + 1: Non-numeric first operand Dumped We can't do math on a list.
🌐
Bookdown
bookdown.org › taragonmd › phds › working-with-lists-and-data-frames.html
Chapter 3 Working with lists and data frames | Population Health Data Science with R
These functions are identical except that sapply “simplifies” the final result, if possible. The do.call function applies a function to the entire list using each each component as an argument. For example, consider a list where each bin contains a vector and we want to cbind the vectors. mylist <- list(vec1 = 1:5, vec2 = 6:10, vec3 = 11:15) cbind(mylist) # will not work · #> mylist #> vec1 Integer,5 #> vec2 Integer,5 #> vec3 Integer,5
🌐
GeeksforGeeks
geeksforgeeks.org › r language › r-lists
R-Lists - GeeksforGeeks
July 12, 2025 - To create a List in R you need to use the function called "list()". We want to build a list of employees with the details.