Victor
Victor

Reputation: 1205

Retrieving minimum non-numeric value

This might be too simple question, but I'm still familiarising with R syntax.

I have a data frame with 2 columns and 3 rows:

Which function should I be using in order to obtain the minimum non-numeric value (i.e. "worse")?

Upvotes: 0

Views: 112

Answers (1)

SimonG
SimonG

Reputation: 4871

Another solution would be to use an ordered factor for the character variable. This way min will know what to do:

dat <- data.frame(a=1:3, b=c("worst","good","best"))                                          
dat$b <- ordered(dat$b, levels=c("worst","good","best"))

min(dat$b)

Result:

> min(dat$b)                                                                                      
[1] worst
Levels: worst < good < best

Upvotes: 1

Related Questions