rovyko
rovyko

Reputation: 4587

Convert factor to a string using a template string in R

If I have a dataframe with values like

d = data.frame(value = runif(1000, 10,50))

and the values are classified into groups using cut

d$class = cut(d$value, 4)

how can quickly convert the factor objects to strings representing the range? For example:

(10,20] -> "10 to 20"

I tried making a function, but I'm wondering if there's a faster way.

style.factor <- function(factors, template="%s to %s") {
  parts = str_split(str_sub(factors, 2, -2), ",", simplify=TRUE)
  return(sprintf(template, parts[,1], parts[,2]))
}
> style.factor(d$class)
  "40 to 50" "20 to 30" "30 to 40" ...

Upvotes: 3

Views: 241

Answers (2)

Brigadeiro
Brigadeiro

Reputation: 2945

You can use sub to do this relatively quickly:

gsub("^\\((.+?),(.+?)\\]$", "\\1 to \\2", d$class)

Upvotes: 3

kangaroo_cliff
kangaroo_cliff

Reputation: 6222

Using an approach similar to yours but with the levels function.

 # extract levels
 levels_c <- levels(d$class)

 # form and assign new levels
 library(stringr)
 parts = str_split(str_sub(levels_c , 2, -2), ",", simplify=TRUE)
 template="%s to %s"
 levels(d$class) <- sprintf(template, parts[,1], parts[,2])

 # check the outcome
 levels(d$class)
 # [1] "10 to 20" "20 to 30" "30 to 40" "40 to 50"

Naturally, if you wish to convert to characters

d$class <- as.character(d$class)  

Upvotes: 2

Related Questions