Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Sometimes it’s useful to write down the various classes of vectors inside your data.frame objects for documentation and other people to use it.
I’ve searched for a quick way to find out all the classes of vectors inside a data.frame.
Since I’ve found no reference for such a function/process I made one up.
I’d like to hear what people have to say about the following use of the “class” function on data.frames
a simple call :
> library(rpart) # comes with R > data(kyphosis) # comes with rpart > class(kyphosis) "data.frame"
trying to use the “apply” function to know what classes are the columns in the data.frame yeilds the following unwanted result :
> apply(kyphosis,2,class) Kyphosis Age Number Start "character" "character" "character" "character"
For some reason the apply function returns “character” on all vectors regardless of their true content (any ideas why?).
Anyhow, after some thought I’ve come up with the following function :
> allClass <- function(x) {unlist(lapply(unclass(x),class))} > allClass(kyphosis) Kyphosis Age Number Start "factor" "integer" "integer" "integer"
Compact, fast and quite useful. Of course the control flow needs more work to fit other classes and recognize when x is not a data.frame.
Comments are welcome.
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.