使用tidyeval的排序因子级别

时间:2018-09-11 15:03:14

标签: r tidyeval

我有一个dataframe,看起来像这样:

my_df <- data.frame(
  x = c("V1", "V2", "V3", "V4"),
  score = c(1, 4, 6, 3),
  order = c(1, 4, 3, 2)
)

my_df$x的级别如下:

> my_df$x
[1] V1 V2 V3 V4
Levels: V1 V2 V3 V4

我希望能够根据x列重新排列order的级别,如下所示:

my_df_ <- my_df %>% 
  mutate(x = factor(x, x[order]))
my_df_$x
[1] V1 V2 V3 V4
Levels: V1 V4 V3 V2

但是我希望能够在使用tidyeval的函数中执行此操作,而对于像我这样的人却无法弄清楚如何...我很高兴能够传递任一字符串("x" )或表达式(x)。

1 个答案:

答案 0 :(得分:2)

使用public changedInput(value){ if(this.model.mac_address){ //we do not need the value, we just update the formatedMac using this.model.mac_address const inputWithourColon = this.model.mac_address.replace(new RegExp(";", 'g'), ""); let blocks = inputWithourColon.match(/.{1,2}/g); let formatedMac = blocks.shift(); for (let block of blocks){ formatedMac = formatedMac + ";" + block; } this.model.mac_address = formatedMac; } } 的潜在解决方案可能是:

forcats