I am learning R right now and using R Studio
I wrote :
library(datasets)
data(mtcars)
## split() function divides the data in a vector. unsplit() function do the reverse.
split(mtcars$mpg, mtcars$cyl)
I get back:
$`4`
[1] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26.0 30.4 21.4
$`6`
[1] 21.0 21.0 21.4 18.1 19.2 17.8 19.7
$`8`
[1] 18.7 14.3 16.4 17.3 15.2 10.4 10.4 14.7 15.5 15.2 13.3 19.2 15.8 15.0
I know that split returns vector. But is this a vector of vectors of length 1?
Visually in R Studio, what is the difference in the display of vector and a matrix?
There are a variety of
is.
functions one of which isYou could simulate is.matrix with:
The notion of a vector from general mathematical perspective and results from
is.vector
are not exactly in alignment. See this earlier response regardingis.vector
. Lists, surprisingly to me anyway, are 'vectors' in R technical parlance. Notice that data.frames which do have a dim attribute are excluded from that category by not being atomic.Here are a few ways to see what the result of
split(calculations..)
is:You can also assing the a new object with the list and interrogate it using the previous functions
By now, it's clear the object split returns is a list. In this case, the list
cars_ls
has 3 numeric vectors. You can index the list in a few ways. Here are some examples. Obviously, there is no matrix here.EDIT Technically speaking, lists are vectors also. Here are a few more functions to check what type of object you have.
Regarding what unlist does:
un_ls
is a numeric vector, clearly not a list. Sounlist()
grabs a list and unlists it.You can find a more detailed description of these functions in the R Language Definition