c在英语中不是向量的缩写,那么为什么要用 c()来定义 R 中的向量呢?
c
c()
v1<- c(1,2,3,4,5)
这是个好问题,答案有点奇怪。“ c”,信不信由你,代表“合并”,通常是这样的:
> c(c(1, 2), c(3)) [1] 1 2 3
但是碰巧在 R 中,一个数只是一个长度为1的向量:
> 1 [1] 1
所以,当你使用 c()来创建一个向量时,你实际上是把一系列1长度的向量组合在一起。
Owen 的答案是完美的,但还有一点需要注意的是 c ()可以连接的不仅仅是向量。
> x = list(a = rnorm(5), b = rnorm(7)) > y = list(j = rpois(3, 5), k = rpois(4, 2), l = rbinom(9, 1, .43)) > foo = c(x,y) > foo $a [1] 0.280503895 -0.853393705 0.323137905 1.232253725 -0.007638861 $b [1] -2.0880857 0.2553389 0.9434817 -1.2318130 -0.7011867 0.3931802 -1.6820880 $j [1] 5 12 5 $k [1] 3 1 2 1 $l [1] 1 0 0 1 0 0 1 1 0 > class(foo) [1] "list"
第二个例子:
> x = 1:10 > y = 3*x+rnorm(length(x)) > z = lm(y ~ x) > is.vector(z) [1] FALSE > foo = c(x, z) > foo [[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] 4 [[5]] [1] 5 [[6]] [1] 6 [[7]] [1] 7 [[8]] [1] 8 [[9]] [1] 9 [[10]] [1] 10 $coefficients (Intercept) x 0.814087 2.813492 $residuals 1 2 3 4 5 6 7 -0.2477695 -0.3375283 -0.1475338 0.5962695 0.5670256 -0.5226752 0.6265995 8 9 10 0.1017986 -0.4425523 -0.1936342 $effects (Intercept) x -51.50810097 25.55480795 -0.05371226 0.66592081 0.61250676 -0.50136423 0.62374031 0.07476915 -0.49375185 -0.26900403 $rank [1] 2 $fitted.values 1 2 3 4 5 6 7 8 3.627579 6.441071 9.254562 12.068054 14.881546 17.695038 20.508529 23.322021 9 10 26.135513 28.949005 $assign [1] 0 1 $qr $qr (Intercept) x 1 -3.1622777 -17.39252713 2 0.3162278 9.08295106 3 0.3162278 0.15621147 4 0.3162278 0.04611510 5 0.3162278 -0.06398128 6 0.3162278 -0.17407766 7 0.3162278 -0.28417403 8 0.3162278 -0.39427041 9 0.3162278 -0.50436679 10 0.3162278 -0.61446316 attr(,"assign") [1] 0 1 $qraux [1] 1.316228 1.266308 $pivot [1] 1 2 $tol [1] 1e-07 $rank [1] 2 attr(,"class") [1] "qr" $df.residual [1] 8 $xlevels named list() $call lm(formula = y ~ x) $terms y ~ x attr(,"variables") list(y, x) attr(,"factors") x y 0 x 1 attr(,"term.labels") [1] "x" attr(,"order") [1] 1 attr(,"intercept") [1] 1 attr(,"response") [1] 1 attr(,".Environment") <environment: R_GlobalEnv> attr(,"predvars") list(y, x) attr(,"dataClasses") y x "numeric" "numeric" $model y x 1 3.379809 1 2 6.103542 2 3 9.107029 3 4 12.664324 4 5 15.448571 5 6 17.172362 6 7 21.135129 7 8 23.423820 8 9 25.692961 9 10 28.755370 10