I'm trying to get the min/max for each column in a large data frame, as part of getting to know my data. My first try was:
apply(t,2,max,na.rm=1)
It treats everything as a character vector, because the first few columns are character types. So max of some of the numeric columns is coming out as " -99.5"
.
I then tried this:
sapply(t,max,na.rm=1)
but it complains about max not meaningful for factors. (lapply
is the same.) What is confusing me is that apply
thought max
was perfectly meaningful for factors, e.g. it returned "ZEBRA" for column 1.
BTW, I took a look at Using sapply on vector of POSIXct and one of the answers says "When you use sapply, your objects are coerced to numeric,...". Is this what is happening to me? If so, is there an alternative apply function that does not coerce? Surely it is a common need, as one of the key features of the data frame type is that each column can be a different type.
The reason that
max
works withapply
is thatapply
is coercing your data frame to a matrix first, and a matrix can only hold one data type. So you end up with a matrix of characters.sapply
is just a wrapper forlapply
, so it is not surprising that both yield the same error.The default behavior when you create a data frame is for categorical columns to be stored as factors. Unless you specify that it is an ordered factor, operations like
max
andmin
will be undefined, since R is assuming that you've created an unordered factor.You can change this behavior by specifying
options(stringsAsFactors = FALSE)
, which will change the default for the entire session, or you can passstringsAsFactors = FALSE
in thedata.frame()
construction call itself. Note that this just means thatmin
andmax
will assume "alphabetical" ordering by default.Or you can manually specify an ordering for each factor, although I doubt that's what you want to do.
Regardless,
sapply
will generally yield an atomic vector, which will entail converting everything to characters in many cases. One way around this is as follows:If it were an "ordered factor" things would be different. Which is not to say I like "ordered factors", I don't, only to say that some relationships are defined for 'ordered factors' that are not defined for "factors". Factors are thought of as ordinary categorical variables. You are seeing the natural sort order of factors which is alphabetical lexical order for your locale. If you want to get an automatic coercion to "numeric" for every column, ... dates and factors and all, then try:
Or if you want to test for factors first and return as you expect then:
@Darrens comment does work better:
max
does succeed with character vectors.If you want to learn your data
summary (df)
provides the min, 1st quantile, median and mean, 3rd quantile and max of numerical columns and the frequency of the top levels of the factor columns.building on @ltamar's answer:
Use summary and munge the output into something useful!
It's not pretty and it is certainly not fast but it gets the job done!