Closed shizidushu closed 6 years ago
Hi @shizidushu,
I don't think this has to do with the character encoding, as the same thing occurs if you do the same thing using vars(ends_with('ty'))
library(tidyverse)
df <- data.frame(qty = c(3,1,5,4), 金额 = c(1,3,4,2))
# normal arrange descending
arrange(df, desc(`金额`))
#> qty 金额
#> 1 5 4
#> 2 1 3
#> 3 4 2
#> 4 3 1
# works
arrange_at(df, vars(ends_with('ty')))
#> qty 金额
#> 1 1 3
#> 2 3 1
#> 3 4 2
#> 4 5 4
# works
arrange_at(df, vars(ends_with('金额')))
#> qty 金额
#> 1 3 1
#> 2 4 2
#> 3 1 3
#> 4 5 4
# works
arrange_at(df, vars(ends_with('ty')))
#> qty 金额
#> 1 1 3
#> 2 3 1
#> 3 4 2
#> 4 5 4
# doesn't work
arrange_at(df, desc(vars(ends_with('ty'))))
#> qty 金额
#> 1 3 1
#> 2 4 2
#> 3 1 3
#> 4 5 4
Created on 2018-09-12 by the reprex package (v0.2.0.9000).
desc()
cannot be applied to vars()
, which specifies the column indices, not expressions. If you want to transform the variables, the transformation functions should be specified on .funs
argument.
library(dplyr, warn.conflicts = FALSE)
df <- data.frame(qty = c(3,1,5,4), 金额 = c(1,3,4,2))
arrange_at(df, vars(ends_with('金额')))
#> qty 金额
#> 1 3 1
#> 2 4 2
#> 3 1 3
#> 4 5 4
arrange_at(df, vars(ends_with('金额')), desc)
#> qty 金额
#> 1 5 4
#> 2 1 3
#> 3 4 2
#> 4 3 1
Created on 2018-09-13 by the reprex package (v0.2.0.9000).
FYI, Examples section of arrange_all
explains this:
# You can supply a function that will be applied before taking the
# ordering of the variables. The variables of the sorted tibble
# keep their original values.
arrange_all(df, desc)
arrange_all(df, funs(desc(.)))
@batpigandme @yutannihilation Thanks for your reply.
So .funs
do the transformation and the transformed values are used to generate the order.
But now I'm confused about what .funs
do when I specify two functions.
library(tidyverse)
df <- data.frame(qty1 = c(1, 1, 2), qty2 = c(-1, 0, -2), amt = c(10, 13, 11))
# arrange_at and their arrange equivalents?
arrange_at(df, vars(starts_with('qty')), funs(desc, identity)) # arrange(df, desc(qty1), qty2)
#> qty1 qty2 amt
#> 1 2 -2 11
#> 2 1 0 13
#> 3 1 -1 10
arrange_at(df, vars(starts_with('qty')), funs(identity, identity)) # arrange(df, qty1, qty2)
#> qty1 qty2 amt
#> 1 1 -1 10
#> 2 1 0 13
#> 3 2 -2 11
arrange_at(df, vars(starts_with('qty')), funs(identity, desc)) # arrange(df, qty1, desc(qty2))
#> qty1 qty2 amt
#> 1 1 -1 10
#> 2 1 0 13
#> 3 2 -2 11
Created on 2018-09-13 by the reprex package (v0.2.0).
IIUC, arrange_at(df, vars(starts_with('qty')), funs(desc, identity))
is equivalent to
arrange(df, desc(qty1), decs(qty2), identity(qty1), identify(qty2))
If you want to transform variables separately, I recommend you to use tidyeval...
@yutannihilation Thanks. I got it.
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/
It seems arranged_at doesn't work properly if Chinese colnames used in select helpers.