[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() 相當于 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)容合作請聯(lián)系作者
  • 序言:七十年代末甥捺,一起剝皮案震驚了整個濱河市为流,隨后出現(xiàn)的幾起案子边败,更是在濱河造成了極大的恐慌减途,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舒萎,死亡現(xiàn)場離奇詭異程储,居然都是意外死亡,警方通過查閱死者的電腦和手機臂寝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門章鲤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人交煞,你說我怎么就攤上這事咏窿。” “怎么了素征?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵集嵌,是天一觀的道長。 經(jīng)常有香客問我御毅,道長根欧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任端蛆,我火速辦了婚禮凤粗,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘今豆。我一直安慰自己嫌拣,他們只是感情好,可當我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布呆躲。 她就那樣靜靜地躺著异逐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪插掂。 梳的紋絲不亂的頭發(fā)上灰瞻,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天腥例,我揣著相機與錄音,去河邊找鬼酝润。 笑死燎竖,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的要销。 我是一名探鬼主播构回,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼疏咐!你這毒婦竟也來了捐凭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤凳鬓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后患民,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體缩举,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年匹颤,在試婚紗的時候發(fā)現(xiàn)自己被綠了仅孩。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡印蓖,死狀恐怖辽慕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情赦肃,我是刑警寧澤溅蛉,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站他宛,受9級特大地震影響船侧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜厅各,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一镜撩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧队塘,春花似錦袁梗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至投放,卻和暖如春奈泪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工涝桅, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留拜姿,地道東北人。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓冯遂,卻偏偏與公主長得像蕊肥,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蛤肌,可洞房花燭夜當晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 韓劇總是套路滿滿裸准,我們觀眾也會邊追邊吐槽“韓劇有三寶展东,車禍、癌癥炒俱、失憶了”盐肃。萬萬沒想到本以為是狗血的偶像劇情節(jié)竟然...
    健康管家閱讀 345評論 0 1
  • It's so good to be understood, yet it feels even better t...
    郭綠獅閱讀 233評論 0 1
  • 所謂自控力,即意志力权悟,也是控制自己的注意力砸王、情緒和欲望的能力。 從失控――自控峦阁,你需要知道什么谦铃? 1.什么是...
    追尋真我閱讀 415評論 0 1
  • 今天不更小說了,可能是最近太幸福了榔昔,小說有點卡驹闰,先停一下,緩緩神件豌。 文|云云先生 今天參加了寶寶的中考誓師大會疮方,會...
    燕小白閱讀 306評論 7 11
  • 妞兒拿著這份作業(yè)給我看時,我是真的有些驚訝茧彤,我一直覺得傻傻長不大的孩子原來也很有自己的想法和見解骡显。 這是英語老師留...
    萱I草閱讀 253評論 0 0