Data Type Conversion
Type conversions in R work as you would expect. For example, adding a character string to a numeric vector converts all the elements in the vector to character.
Use is.foo to test for data type foo. Returns TRUE or FALSE
Use as.foo to explicitly convert it.
is.numeric(), is.character(), is.vector(), is.matrix(), is.data.frame()
as.numeric(), as.character(), as.vector(), as.matrix(), as.data.frame)
Examples
to one long vector |
to matrix |
to data frame |
|
from vector |
c(x,y) |
cbind(x,y) rbind(x,y) |
data.frame(x,y) |
from matrix |
as.vector(mymatrix) | as.data.frame(mymatrix) | |
from data frame |
as.matrix(myframe) |
Dates
You can convert dates to and from character or numeric data. See date values for more information.
To Practice
To explore data types in R, try this free interactive introduction to R course.