Data Class Conversion

[This article was first published on Analysis with R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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,

Consider this data,

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,

To leave a comment for the author, please follow the link and comment on their blog: Analysis with R.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)