Why use c() to define vector?
c
is not the abbreviation of vector in English, so why use c()
to define a vector in R?
v1<- c(1,2,3,4,5)
Solution 1:
This is a good question, and the answer is kind of odd. "c", believe it or not, stands for "combine", which is what it normally does:
> c(c(1, 2), c(3))
[1] 1 2 3
But it happens that in R, a number is just a vector of length 1:
> 1
[1] 1
So, when you use c()
to create a vector, what you are actually doing is combining together a series of 1-length vectors.
Solution 2:
Owen's answer is perfect, but one other thing to note is that c() can concatenate more than just vectors.
> 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"
Second Example:
> 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