Friday, 7 June 2013

R: Data Class Conversion

Data in R can be converted from one class to the other. The functions are prefixed with as. then followed by the name of the data class that we wish to convert to. Data class in R are the following:
  • numeric - as.numeric;
  • vector - as.vector;
  • character - as.character;
  • matrix - as.matrix; and,
  • data frame - as.data.frame.
Hence, if one wishes to convert a numeric data points 32, 35, 38, 29, 27, 40, and 33 into a character. Then, this is achieved by

Notice the difference between the output of the data object and the converted one, data.ch? The output differs only with this character, ". This character that encloses every data points suggests that the data is now in character form. And this can be verified using the function class,

Now consider this,

These objects are in numeric class, and converting these to data frame with three variables, would be

Further to matrix class, we have


No comments:

Post a Comment