【问题标题】:Quasiquotation Error: incorrect size (1) at position 1, expecting : 144000Quasiquotation 错误:位置 1 的大小 (1) 不正确,预期为:144000
【发布时间】:2019-06-24 22:55:58
【问题描述】:

我想将列列表传递给排列函数。我尝试使用 enquos 函数

column.names 在哪里

column.names <- c(colnames(SQL_Table))
column.names
 [1] "plan"           "class"          "gender"         "band"           "marital_status" "acceleration"   "extension"     
 [8] "inflation"      "iss_age"        "cell"           "dur"            "db_perk"        "accel_perk"     "ext_perk"      
[15] "attage" 

我的代码是

column.names <- c(colnames(SQL_Table))

arrange.remove <- c("cell","db_perk","accel_perk","ext_perk","attage","db_perk_compare")
arrange.columns <- setdiff(column.names,remove)
arrange_quo <-  enquos(arrange.columns)

SQL_Table %>%
  arrange(!!arrange_quo)

我收到了错误

Error: incorrect size (1) at position 1, expecting : 144000

【问题讨论】:

  • 你需要rlang::syms而不是enquos和拼接运算符!!!

标签: r dplyr rlang


【解决方案1】:

根据这个post你可以使用构造:

cols.names <- colnames(df)
df[do.call('order', df[cols.names]), ]
df[do.call('order', c(df[cols.names], list(decreasing=TRUE))), ]

所以在使用管道的情况下:

df %>% 
  .[do.call("order", .[cols.names]), ]

但是对于单列名称也可以:

col.name <- "column.name"
df %>%
  arrange(.[[get("col.name")]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-13
    • 2015-04-24
    • 2022-11-18
    • 2017-06-02
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多