R语言入门(一)
一 data structure
1.vector: a single entity consisting of an ordered collection of numbers
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
等价于 c(10.4, 5.6, 3.1, 6.4, 21.7) -> x
命名变量还可以使用assign,assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7)),二者等价
向量相加减,如果不是一样长的会自动补齐(重复自己一直到达到最长向量的长度)
v <- 2*x + y + 1
其他操作
length(x)
sum(x)
prod(x) #乘法
mean(x)
var(x)=sum((x-mean(x))^2)/(length(x)-1)
注:If the argument to var() is an n-by-p matrix the value is a p-by-p sample covariance matrix got by regarding the rows as independent p-variate sample vectors
如果方差函数里的x是n*p矩阵,则计算方差的时候采用列的p维,把列当作独立的p维向量来计算方差
sort(x) #只是进行排序操作并没有改变x本身
sort(x,decreasing = TRUE) #还可以倒序排列