R中一切都是vector

时间:2024-11-15 11:34:49

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。比如

 > fac<- factor(c("b", "a", "b"))
> dput(fac)
structure(c(2L, 1L, 2L), .Label = c("a", "b"), class = "factor")
> levels(fac)
[] "a" "b"
> fac
[] b a b
Levels: a b
> as.numeric(fac)
[]
factor组成: integer vector+ levels attributes

1、arrary和vecotr的转换

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

>nota=array(:,)//只有一个dimension 的array
>dim(not1)<-NULL
>dput(nota)
:
>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, , "a")
> is.vector(vl)
[] TRUE
> class(vl)
[] "list"
> attributes(vl)
NULL
注意names 不是属性的,所以namedlist依旧是vector
> my.list <- list(num=:, let=LETTERS[:])
> names(my.list)
[] "num" "let"
> is.vector(my.list)
[] TRUE