How can I add a vector to a list of vectors in R? - Stack Overflow
Convert a list of lists into a list of vectors? `Unlist()` is not working
Making a vector of lists?
How to make a list of integer vectors in R - Stack Overflow
Try this with double bracket for lists as @Nicola suggested in comments:
#Data
e <- list(c(0,1),c(1,3),c(4,0))
#Add
e[[4]] <- c(5,3)
Output:
e
[[1]]
[1] 0 1
[[2]]
[1] 1 3
[[3]]
[1] 4 0
[[4]]
[1] 5 3
It can also work:
#Code2
e <- c(e,list(c(5,3)))
You should use list when with [
e[4] <- list(c(5, 3))
since [ store data in terms of list in your case.
Or just [[ for assignment
e[[4]] <- c(5,3)
You can type ?[ and read for more information.
With `unlist()`
Solution:
I was unable to create list of vectors but if anyone comes across this in the future, I used `as.vector()` inside a loop as a makeshift solution for this.
I'm very new to the language and currently ramming my head against the wall because of an issue I can't seem to solve.
I want to make a dataframe with two vectors of five overall items. However, each of the items of the first vector has to be assigned a varying number of valued (so item A has to be assigned 2 values, item B has to be assigned 5 values, etc.).
If I put all of these chars into the second vector without any other changes, the vector will obviously be of a different length and I can't make a dataframe out of it. So I've tried instead making five lists of the values and making those into a vector of lists, which hasn't worked either.
I've been thinking about this for the netter part of an hour now and also looked online elsewhere to no avail (also because I honestly don't know how to properly paraphrase this problem).
According to ?"[" (under the section "recursive (list-like) objects"):
Indexing by โ[โ is similar to atomic vectors and selects a list of
the specified element(s).
Both โ[[โ and โ$โ select a single element of the list. The main
difference is that โ$โ does not allow computed indices, whereas
โ[[โ does. โx$nameโ is equivalent to โx[["name", exact =
FALSE]]โ. Also, the partial matching behavior of โ[[โ can be
controlled using the โexactโ argument.
Basically, for lists, [ selects more than one element, so the replacement must be a list (not a vector as in your example). Here's an example of how to use [ on lists:
l <- list(c(1,2,3), c(4,5,6))
l[1] <- list(1:2)
l[1:2] <- list(1:3,4:5)
If you only want to replace one element, use [[ instead.
l[[1]] <- 1:3
Use [[1]] as in
l[[1]] <- c(1,2,3)
l[[2]] <- 1:4
and so. Also recall that preallocation is much more efficient, so if you know how long your list is going to be, use something like
l <- vector(mode="list", length=N)
Lists allow data objects to have an arbitrary structure with diverse data types. Atomic vectors can be indexed by one dimension (which allows speedier access to values) and can hold only one class of item at on time. Assignment of an item with a different type may result in coercion of the entire atomic vector to the new data type. Matrix or array classed objects are atomic with an additional dimension attribute. Technically lists are vectors, although very few would use that term. "list" is one of several modes, with others being "logical", "character", "numeric", "integer". What you are calling vectors are "atomic vectors" in strict R parlance:
aaa <- vector("list", 3)
is.list(aaa) #TRUE
is.vector(aaa) #TRUE
Lists are a "recursive" type (of vector) whereas atomic vectors are not:
is.recursive(aaa) # TRUE
is.atomic(aaa) # FALSE
You process data objects with different functions depending on whether they are recursive, atomic or have dimensional attributes (matrices and arrays). However, I'm not sure that a discussion of the "advantages and disadvantages" of different data structures is a sufficiently focused question for SO. To add to what Tommy said, besides lists being capable of holding an arbitrary number of other vectors there is the availability of dataframes which are a particular type of list that has a dimensional attribute which defines its structure. Unlike matrices and arrays which are really folded atomic objects, dataframes can hold varying types including factor type.
S4 objects are special list types with associated functional requirements.
The introduction of the โtibbleโ data types in the tidyverse adds enhanced flexibility to what was a rather limited capacity for columns containing lists by the dataframe type in base R.
There's also the caveat that the is.vector function will return FALSE when there are attributes other than names. See: what is vector?
Lists are "recursive". This means that they can contain values of different types, even other lists:
x <- list(values=sin(1:3), ids=letters[1:3], sub=list(foo=42,bar=13))
x # print the list
x$values # Get one element
x[["ids"]] # Another way to get an element
x$sub$foo # Get sub elements
x[[c(3,2)]] # Another way (gets 13)
str(x) # A "summary" of the list's content
Lists are used in R to represent data sets: the data.frame class is essentially a list where each element is a column of a specific type.
Another use is when representing a model: the result from lm returns a list that contains a bunch of useful objects.
d <- data.frame(a=11:13, b=21:23)
is.list(d) # TRUE
str(d)
m <- lm(a ~ b, data=d)
is.list(m) # TRUE
str(m)
Atomic vectors (non-list like, but numeric, logical and character) are useful since all elements are known to have the same type. This makes manipulating them very fast.