rstring

Split version numbers to find latest version


I am working with version numbers and need to pull the latest version. In this case it is 1.11.3. I have a working solution, but I am hoping there is a more elegant R way of doing this. Here is my current solution with a small data set. Does anyone have a better way of doing this? Preferably something with base functions only.

test <- c("0.6.0", "1.0.3", "1.6.7", "1.8.0", "1.5.4", "1.10.0", "1.2.0", "1.11.3", "1.9.4")
split_test <- do.call(rbind, strsplit(test, ".", fixed=TRUE))
split_test <- matrix(as.numeric(split_test), ncol = ncol(split_test))
paste(split_test[order(-split_test[,1], -split_test[,2], -split_test[,3])[1],], collapse=".")

Solution

  • R has a built in function for version parsing: numeric_version. You can use

    max(numeric_version(test))
    # [1] ‘1.11.3’