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
我当记事本用的