【问题标题】:how to return a value from factored intervals of create_qgroups() in r如何从 r 中 create_qgroups() 的因式间隔返回一个值
【发布时间】:2021-12-10 06:51:48
【问题描述】:

我有一个数据框。我根据其值创建了一个分位数组。

 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)

所以输出是

(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]

我想要一个向量,其值在每个间隔中都是最大值 像 (566,346,747,...,inf) 但 my_vec 是因素。我不知道该怎么做。

【问题讨论】:

  • 也包含这个函数,所以我们可以玩弄结果因子的结构;你在使用 findInterval 吗?
  • 我正在使用 create_qgroups() 这给了我基于分位数的区间。如上面的帖子所示。
  • 这不是基本的 R 函数。你必须说明你从哪里得到的。我查了一下,它来自{dvmisc}

标签: r vector numeric quantile


【解决方案1】:

这是一种提取区间右边部分的方法:

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)

【讨论】:

  • 谢谢,但这给出了 346 566 747 Inf,这与我想要的 (566,346,747,...,inf) 明显不同
  • 是的,如果您想要因子中每个值的右侧,我已将 levels() 部分注释掉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多