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

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

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

问题

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

以下是一个示例:

perc_type <- c('100%: a', '200%: a', '300%: a', '50%: a', '100%: bx', '200%: bx', '300%: bx', '50%: bx',
               '100%: cy', '200%:cy', '300%: cy', '50%: cy' )
country <- c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'c', 'c', 'c', 'c')

test <- data.frame(perc_type, country)

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

perc_type  country
100%: a     A
200%: a     A
300%: a     A
50%: a      A
100%: bx    B
200%: bx    B
300%: bx    B
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:

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;,
               &#39;100%: cy&#39;, &#39;200%:cy&#39;, &#39;300%: cy&#39;, &#39;50%: cy&#39; )
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;)

test &lt;- data.frame(perc_type, country)

Now I am getting this:

perc_type  country
100%: a     A
200%: a     A
300%: a     A
50%: a      A
100%: bx    B
200%: bx    B
300%: bx    B
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中,你可以按照以下方式操作 -

test[with(test, order(country, as.integer(sub('%.*', '', perc_type)))), ]

#   perc_type country
#4     50%: a       A
#1    100%: a       A
#2    200%: a       A
#3    300%: a       A
#8    50%: bx       B
#5   100%: bx       B
#6   200%: bx       B
#7   300%: bx       B
#12   50%: cy       c
#9   100%: cy       c
#10   200%:cy       c
#11  300%: cy       c

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


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

library(dplyr)

test %>%
  mutate(perc_value = readr::parse_number(perc_type)) %>%
  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 -

test[with(test, order(country, as.integer(sub(&#39;%.*&#39;, &#39;&#39;, perc_type)))), ]

#   perc_type country
#4     50%: a       A
#1    100%: a       A
#2    200%: a       A
#3    300%: a       A
#8    50%: bx       B
#5   100%: bx       B
#6   200%: bx       B
#7   300%: bx       B
#12   50%: cy       c
#9   100%: cy       c
#10   200%:cy       c
#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 -

library(dplyr)

test %&gt;%
  mutate(perc_value = readr::parse_number(perc_type)) %&gt;%
  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:

确定