重新调整具有不同值的因子基于其他列的值的组。

huangapple go评论95阅读模式
英文:

relevel factor with different values' groups based on other column values

问题

我需要渲染一个表格,并且需要根据另一列的值对其中一列进行排序或重新排序,以便我可以获得第二列每个值的正确顺序。

以下是一个示例:

  1. perc_type <- c('100%: a', '200%: a', '300%: a', '50%: a', '100%: bx', '200%: bx', '300%: bx', '50%: bx',
  2. '100%: cy', '200%:cy', '300%: cy', '50%: cy' )
  3. country <- c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'c', 'c', 'c', 'c')
  4. test <- data.frame(perc_type, country)

现在我得到的是这样的结果:

  1. perc_type country
  2. 100%: a A
  3. 200%: a A
  4. 300%: a A
  5. 50%: a A
  6. 100%: bx B
  7. 200%: bx B
  8. 300%: bx B
  9. 50%: bx B

但是我希望对于每个字母,按百分比递增的顺序排列为50%、100%、200%、300%,是否可以实现?
谢谢!

英文:

I need to render a table and I need to order or relevel one column based on the values of the other column, so that I get the correct order for each value of the second column.

here is and example:

  1. perc_type &lt;- c(&#39;100%: a&#39;, &#39;200%: a&#39;, &#39;300%: a&#39;, &#39;50%: a&#39;, &#39;100%: bx&#39;, &#39;200%: bx&#39;, &#39;300%: bx&#39;, &#39;50%: bx&#39;,
  2. &#39;100%: cy&#39;, &#39;200%:cy&#39;, &#39;300%: cy&#39;, &#39;50%: cy&#39; )
  3. country &lt;- c(&#39;A&#39;, &#39;A&#39;, &#39;A&#39;, &#39;A&#39;, &#39;B&#39;, &#39;B&#39;, &#39;B&#39;, &#39;B&#39;, &#39;c&#39;, &#39;c&#39;, &#39;c&#39;, &#39;c&#39;)
  4. test &lt;- data.frame(perc_type, country)

Now I am getting this:

  1. perc_type country
  2. 100%: a A
  3. 200%: a A
  4. 300%: a A
  5. 50%: a A
  6. 100%: bx B
  7. 200%: bx B
  8. 300%: bx B
  9. 50%: bx B

but I would like that for each letter the order would be with increasing percentage 50% 100% 200% 300%,
is it possible to do it?
Thanks!

答案1

得分: 2

你可以从 perc_type 列中提取数值,并在 order 中使用它来获取所需的排序顺序。

在基本的R中,你可以按照以下方式操作 -

  1. test[with(test, order(country, as.integer(sub('%.*', '', perc_type)))), ]
  2. # perc_type country
  3. #4 50%: a A
  4. #1 100%: a A
  5. #2 200%: a A
  6. #3 300%: a A
  7. #8 50%: bx B
  8. #5 100%: bx B
  9. #6 200%: bx B
  10. #7 300%: bx B
  11. #12 50%: cy c
  12. #9 100%: cy c
  13. #10 200%:cy c
  14. #11 300%: cy c

其中 sub('%.*', '', perc_type) 删除了百分号之后的所有内容,所以我们只剩下数字。


使用 dplyr,这是另一种选项 -

  1. library(dplyr)
  2. test %>%
  3. mutate(perc_value = readr::parse_number(perc_type)) %>%
  4. arrange(country, perc_value)
英文:

You may extract the numeric value from perc_type column and use it in order to get the required order.

In base R, you can do that as following -

  1. test[with(test, order(country, as.integer(sub(&#39;%.*&#39;, &#39;&#39;, perc_type)))), ]
  2. # perc_type country
  3. #4 50%: a A
  4. #1 100%: a A
  5. #2 200%: a A
  6. #3 300%: a A
  7. #8 50%: bx B
  8. #5 100%: bx B
  9. #6 200%: bx B
  10. #7 300%: bx B
  11. #12 50%: cy c
  12. #9 100%: cy c
  13. #10 200%:cy c
  14. #11 300%: cy c

where sub(&#39;%.*&#39;, &#39;&#39;, perc_type) drops everything after a &quot;%&quot; sign so we are only left with numbers.


Using dplyr, here is another option -

  1. library(dplyr)
  2. test %&gt;%
  3. mutate(perc_value = readr::parse_number(perc_type)) %&gt;%
  4. arrange(country, perc_value)

huangapple
  • 本文由 发表于 2023年7月31日 19:18:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定