76

c在英文中不是vector的缩写,那为什么c()在R中定义一个vector呢?

v1<- c(1,2,3,4,5)
4

2 回答 2

88

这是一个很好的问题,但答案有点奇怪。信不信由你,“c”代表“组合”,这是它通常所做的:

> c(c(1, 2), c(3))
[1] 1 2 3

但碰巧在 R 中,数字只是长度为 1 的向量:

> 1
[1] 1

因此,当您使用c()创建向量时,您实际上正在做的是将一系列长度为 1 的向量组合在一起。

于 2012-07-15T02:14:43.320 回答
27

欧文的回答很完美,但要注意的另一件事是 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
于 2012-07-15T02:59:51.673 回答