[R]-Data structures

Cite: http://adv-r.had.co.nz/Data-structures.html

R's base data structures can be organised by their dimensionality (1d, 2d, or nd) and whether they're homogeneous (all contents must be of the same type) or heterogeneous (the contents can be of different types). This gives rise to the five data types most often used in data analysis:

Homogeneous Heterogeneous
1d (vector) Atomic vector List
2d Matrix Data frame
nd Array -

Note that R has no 0-dimensional, or scalar types. Individual numbers or strings, which you might think would be scalars, are actually vectors of length one.

Given an object, the best way to understand what data structures it’s composed of is to use str():

vector and matrix are just aliases for one- and two-dimensional array respectively.

Vector

The basic data structure in R is the vector. Vectors come in two flavours: atomic vector and list. They have three common properties:

  • Type, typeof(), what it is.
  • Length, length(), how many elements it contains.
  • Attributes, attributes(), additional arbitrary metadata.
Atomic vector

There are four common types of atomic vectors: logical, integer, double (often called numeric), and character. There are two rare types that I will not discuss further: complex and raw. Atomic vectors are usually created with c(), short for combine.

Atomic vectors are always flat, even if you nest c()’s:

c(1, c(2, c(3, 4)))
#> [1] 1 2 3 4
# the same as
c(1, 2, 3, 4)
#> [1] 1 2 3 4

Given a vector, you can determine its type with typeof(), or check if it's a specific type with an "is" function:

is.character()
is.double()
is.integer()
is.logical()
# or, more generally
is.atomic()

# examples
int_var <- c(1L, 6L, 10L)
typeof(int_var)
#> [1] "integer"
is.integer(int_var)
#> [1] TRUE
is.atomic(int_var)
#> [1] TRUE

dbl_var <- c(1, 2.5, 4.5)
typeof(dbl_var)
#> [1] "double"
is.double(dbl_var)
#> [1] TRUE
is.atomic(dbl_var)
#> [1] TRUE

is.numeric() 相當(dāng)于 is.integer() | is.double():

is.numeric(int_var)
#> [1] TRUE
is.numeric(dbl_var)
#> [1] TRUE
List

You construct lists by using list() instead of c():

x <- list(1:3, "a", c(TRUE, FALSE, TRUE), c(2.3, 5.9))
str(x)
#> List of 4
#>  $ : int [1:3] 1 2 3
#>  $ : chr "a"
#>  $ : logi [1:3] TRUE FALSE TRUE
#>  $ : num [1:2] 2.3 5.9

Lists are sometimes called recursive vectors, because a list can contain other lists:

x <- list(list(list(list())))
str(x)
#> List of 1
#>  $ :List of 1
#>   ..$ :List of 1
#>   .. ..$ : list()
is.recursive(x)
#> [1] TRUE

c() will combine several lists into one. If given a combination of atomic vectors and lists, c() will coerce the vectors to lists before combining them. Compare the results of list() and c():

x <- list(list(1, 2), c(3, 4))
y <- c(list(1, 2), c(3, 4))
str(x)
#> List of 2
#>  $ :List of 2
#>   ..$ : num 1
#>   ..$ : num 2
#>  $ : num [1:2] 3 4
str(y)
#> List of 4
#>  $ : num 1
#>  $ : num 2
#>  $ : num 3
#>  $ : num 4

You can turn a list into an atomic vector with unlist(). If the elements of a list have different types, unlist() uses the same coercion rules as c().

Lists are used to build up many of the more complicated data structures in R. For example, both data frames (described in data frames) and linear models objects (as produced by lm()) are lists:

is.list(mtcars)
#> [1] TRUE

mod <- lm(mpg ~ wt, data = mtcars)
is.list(mod)
#> [1] TRUE
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 韓劇總是套路滿滿,我們觀眾也會(huì)邊追邊吐槽“韓劇有三寶,車禍、癌癥、失憶了”。萬(wàn)萬(wàn)沒(méi)想到本以為是狗血的偶像劇情節(jié)竟然...
    健康管家閱讀 456評(píng)論 0 1
  • It's so good to be understood, yet it feels even better t...
    郭綠獅閱讀 256評(píng)論 0 1
  • 所謂自控力,即意志力,也是控制自己的注意力、情緒和欲望的能力。 從失控――自控,你需要知道什么? 1.什么是...
    追尋真我閱讀 484評(píng)論 0 1
  • 今天不更小說(shuō)了,可能是最近太幸福了,小說(shuō)有點(diǎn)卡,先停一下,緩緩神。 文|云云先生 今天參加了寶寶的中考誓師大會(huì),會(huì)...
    燕小白閱讀 389評(píng)論 7 11
  • 妞兒拿著這份作業(yè)給我看時(shí),我是真的有些驚訝,我一直覺(jué)得傻傻長(zhǎng)不大的孩子原來(lái)也很有自己的想法和見(jiàn)解。 這是英語(yǔ)老師留...
    萱I草閱讀 330評(píng)論 0 0

友情鏈接更多精彩內(nèi)容