R中一切都是vector


0、可以說R語言中一切結構體的基礎是vector!

R中一切都是vector,vecotor的每個component必須類型一致(character,numeric,integer....)!
vector 是沒有dimensions的也沒有attributes,所以去掉dimension和attributes就成了vector(其實dimension可以作為attributes的一個屬性存在但是named** 命名**一般不會作為attributes的屬性的)
解釋下

0.1為何沒有dimensions?

------這里其實糾正我一個觀點:我一直以一個點是有維度的,但其實點是標量

What I don't understand is why vectors (with more than one value) don't 
have dimensions. They look like they do have 1 dimension. For me no 
dimension 沒有dimension就是標量,比如一個點就是標量. Like in geometry: a point has no dimension, 
a line has 1, a square has 2, a cube 3 and so on. Is it because of some 
internal process? The intuitive geometry way of thinking is not 
programmatically relevant?

0.2、沒有attributes,如果加了attributes怎么樣?

factors是character data,但是要編碼成 numeric mode,每個number 關聯一個指定的string,稱為levels。比如

 1 > fac<- factor(c("b", "a", "b"))
 2 > dput(fac)
 3 structure(c(2L, 1L, 2L), .Label = c("a", "b"), class = "factor")
 4 > levels(fac)
 5 [1] "a" "b"
 6 > fac
 7 [1] b a b
 8 Levels: a b
 9 > as.numeric(fac)
10 [1] 2 1 2
11 factor組成: integer vector+ levels attributes

1、arrary和vecotr的轉換

if we remove dimension part array just a vector----array去掉屬性dimension就是vector

>nota=array(1:4,4)//只有一個dimension 的array
>dim(not1)<-NULL
>dput(nota)
1:4
>is.array(nota)
>is.vector(nota)

2、array和matrix的轉換

arrays are matrices with more than 2 dimensions.<==>matrices are arrays with only 2 dimensions
Arrays can have any number of dimensions including 1, 2, 3, etc.
比如只有一個維度的array nota=array(1:4,4)

3、list和vecotor的轉換a list with no attributes is a vecotor,所以如下沒有設置屬性的list是vector 的

> vl<- list(sin, 3, "a")
> is.vector(vl)
[1] TRUE
> class(vl)
[1] "list"
> attributes(vl)
NULL
注意names 不是屬性的,所以namedlist依舊是vector
> my.list <- list(num=1:3, let=LETTERS[1:2])
> names(my.list)
[1] "num" "let"
> is.vector(my.list)
[1] TRUE

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM