Information Technology Reference
In-Depth Information
If the arguments to c(...) are themselves vectors, it flattens them and combines them
into a single vector:
> v1 <- c(1,2,3)
> v2 <- c(4,5,6)
> c(v1,v2)
[1] 1 2 3 4 5 6
Vectors cannot contain a mix of data types, such as numbers and strings. If you create
a vector from mixed elements, R will try to accommodate you by converting one of
them:
> v1 <- c(1,2,3)
> v3 <- c("A","B","C")
> c(v1,v3)
[1] "1" "2" "3" "A" "B" "C"
Here, the user tried to create a vector from both numbers and strings. R converted all
the numbers to strings before creating the vector, thereby making the data elements
compatible.
Technically speaking, two data elements can coexist in a vector only if they have the
same mode . The modes of 3.1415 and "foo" are numeric and character, respectively:
> mode(3.1415)
[1] "numeric"
> mode("foo")
[1] "character"
Those modes are incompatible. To make a vector from them, R converts 3.1415 to
character mode so it will be compatible with "foo" :
> c(3.1415, "foo")
[1] "3.1415" "foo"
> mode(c(3.1415, "foo"))
[1] "character"
c is a generic operator, which means that it works with many datatypes
and not just vectors. However, it might not do exactly what you expect,
so check its behavior before applying it to other datatypes and objects.
1.8 Computing Basic Statistics
Problem
You want to calculate basic statistics: mean, median, standard deviation, variance,
correlation, or covariance.
Solution
Use one of these functions as appropriate, assuming that x and y are vectors:
 
Search WWH ::




Custom Search