COERCION

Jorge Luis Villlaba Acevedo
as.type()

Algunas Coerciones.

Algunos de los tipos de datos en R admiten su conversion a otros tipos; para ello, el lenguaje provee de un conjunto de funciones de la forma: as.“tipo”().

  • as.factor()
  • as.ordered()
  • as.integer()
  • as.double()
  • as.numeric()
  • as.complex()
  • as.logical()
  • as.character()
  • as.vector()
  • as.matrix()
  • as.data.frame()
  • as.array()

Variables cualitativas.

Sea el vector \( A = <\text{z,a,m}> \).

A <- c("z","a","m")
x <- as.factor(A);x
[1] z a m
Levels: a m z
y <- as.ordered(A);y
[1] z a m
Levels: a < m < z
z <- as.integer(A);z
[1] NA NA NA

Variables cuantitativas.

Sea el vector \( B = <0,0.5,1,1.5,2,2.5,3> \).

 B <- seq(0,3,by=0.5);class(B)
[1] "numeric"
 c <- as.integer(B); c
[1] 0 0 1 1 2 2 3
 d <- as.double(B);d
[1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0
 e <- as.character(B);e
[1] "0"   "0.5" "1"   "1.5" "2"   "2.5" "3"  

Otros casos.

h <- matrix(c(3,4,5,6,7,8),2); h
     [,1] [,2] [,3]
[1,]    3    5    7
[2,]    4    6    8
j <- as.data.frame(h);j
  V1 V2 V3
1  3  5  7
2  4  6  8
k <- as.matrix(j);k
     V1 V2 V3
[1,]  3  5  7
[2,]  4  6  8
m <- table( B <- seq(0,3,by=0.5));m

  0 0.5   1 1.5   2 2.5   3 
  1   1   1   1   1   1   1 
n <- as.data.frame(m);n
  Var1 Freq
1    0    1
2  0.5    1
3    1    1
4  1.5    1
5    2    1
6  2.5    1
7    3    1