Data Class Conversion
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Data in R can be converted from one class to the other. The function is 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
- 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 variable 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,
These objects are in numeric class, and converting these to data frame with three variables, would be
And this can be further converted to matrix class,
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.