Home > Software engineering >  how to return a value from factored intervals of create_qgroups() in r
how to return a value from factored intervals of create_qgroups() in r

Time:10-25

I have a data frame. I create a quantile group based on its values.

 library(dvmisc)
 my_vec <- c(415.5326,   138.4831,   658.7316,   737.5037,   773.5543, 
 2062.0428,    79.7115,   473.2346) 
 my_q_grp <- create_qgroups(my_vec, groups = 4)

so out put is

(346,566]  [-Inf,346] (566,747]  (566,747]  (747, Inf] (747, Inf] [-Inf,346] (346,566] 
Levels: [-Inf,346] (346,566] (566,747] (747, Inf]

I want a vector whose values are max value in each interval like (566,346,747,...,inf) but my_vec is factor. I do not know how to do this.

CodePudding user response:

This is a way to extract the right part of intervals:

right_part <- .  %>% 
  #' remove this if you want to do it on each value of the factor
  # levels() %>% 
  str_match("[(\\[](. )[,](. )[)\\]]") %>% 
  `[`(,3) %>% 
  as.numeric()
right_part(my_q_grp)
  • Related