本文介绍了按降序排列_() 多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试将 arrange_() 与字符串输入一起使用,并按降序在其中一列中使用.

library(dplyr) # R version 3.3.0 (2016-05-03) , dplyr_0.4.3# 数据set.seed(1)df1 <- data.frame(grp = factor(c(1,2,1,2,1)),x = 轮(runif(5,1,10),2))# grp x# 1 1 3.39# 2 2 4.35# 3 1 6.16# 4 2 9.17# 5 1 2.82

以下是我需要实现的目标:

df1 %>% 排列(grp,-x)df1 %>% 排列(grp, desc(x))# grp x# 1 1 6.16# 2 1 3.39# 3 1 2.82# 4 2 9.17# 5 2 4.35

在我的例子中,第二列是一个字符串:

#动态字符串myCol % 排列_("grp", desc(myCol))

错误:大小不正确(1),期望:5

df1%>%arrange_("grp", "desc(myCol)")

错误:找不到对象myCol"

df1%>%arrange_(c("grp", "desc(myCol)"))#错误的输出# grp x# 1 1 3.39# 2 1 6.16# 3 1 2.82# 4 2 4.35# 5 2 9.17

我在此处找到了类似的解决方案,但无法使其发挥作用:

df1%>%arrange_(.dots = c("grp", "desc(myCol)"))

错误:找不到对象myCol"

感觉好像我遗漏了一些非常明显的东西,想法?

解决方案

我们可以粘贴 'desc' 作为字符串来评估它.

myCol1 

或者用'myCol'

df1 %>%排列_(.dots = c(grp",paste0(desc(",myCol,)")))

或者使用lazyeval

库(lazyeval)df1%>%排列_(.dots = c(grp", interp(~ desc(n1), n1 = as.name(myCol))))# grp x#1 1 6.16#2 1 3.39#3 1 2.82#4 2 9.17#5 2 4.35


通过使用"desc(myCol)",它是一个单独的字符串,并且不计算myCol"的值.

更新

或者另一个选项是 parse_expr(来自 rlang)并用 !!

求值

df1 %>%安排(grp,!! rlang::parse_expr(myCol1))#grp x#1 1 6.16#2 1 3.39#3 1 2.82#4 2 9.17#5 2 4.35


或者使用 OP 帖子中的原始字符串.将字符串转换为符号(sym),求值(!!)并按降序(desc)排列

myCol 

I am trying to use arrange_() with string input and in one of the columns in descending order.

library(dplyr) # R version 3.3.0 (2016-05-03) , dplyr_0.4.3
# data
set.seed(1)
df1 <- data.frame(grp = factor(c(1,2,1,2,1)),
                  x = round(runif(5,1,10), 2))

#   grp    x
# 1   1 3.39
# 2   2 4.35
# 3   1 6.16
# 4   2 9.17
# 5   1 2.82

Below is what I need to achieve:

df1 %>% arrange(grp, -x)
df1 %>% arrange(grp, desc(x))
#   grp    x
# 1   1 6.16
# 2   1 3.39
# 3   1 2.82
# 4   2 9.17
# 5   2 4.35

In my case second column is a string:

#dynamic string
myCol <- "x"

#failed attempts
df1 %>% arrange_("grp", desc(myCol))
df1 %>% arrange_("grp", "desc(myCol)")
df1 %>% arrange_(c("grp", "desc(myCol)"))
#wrong output
#   grp    x
# 1   1 3.39
# 2   1 6.16
# 3   1 2.82
# 4   2 4.35
# 5   2 9.17

I found similar solution here, but couldn't make it work:

df1 %>% arrange_(.dots = c("grp", "desc(myCol)"))

Feels like I am missing something very obvious, ideas?

解决方案

We can paste 'desc' as a string to evaluate it.

myCol1 <- paste0("desc(", "x)")
df1 %>%
     arrange_(.dots = c("grp", myCol1))
#  grp    x
#1   1 6.16
#2   1 3.39
#3   1 2.82
#4   2 9.17
#5   2 4.35

Or with 'myCol'

df1 %>%
      arrange_(.dots = c("grp", paste0("desc(", myCol, ")")))

Or use lazyeval

library(lazyeval)
df1 %>%
     arrange_(.dots = c("grp", interp(~ desc(n1), n1 = as.name(myCol))))
#  grp    x
#1   1 6.16
#2   1 3.39
#3   1 2.82
#4   2 9.17
#5   2 4.35


By using "desc(myCol)", it is a single string and the value of the 'myCol' is not evaluated.

Update

Or another option is parse_expr (from rlang) and evaluate with !!

df1 %>%
    arrange(grp, !! rlang::parse_expr(myCol1))
#grp    x
#1   1 6.16
#2   1 3.39
#3   1 2.82
#4   2 9.17
#5   2 4.35


Or using the original string in the OP's post. Convert the string to symbol (sym), evaluate (!!) and arrange it in descending (desc) order

myCol <- "x"
df1 %>%
    arrange(grp, desc(!! rlang::sym(myCol)))
# grp    x
#1   1 6.16
#2   1 3.39
#3   1 2.82
#4   2 9.17
#5   2 4.35





这篇关于按降序排列_() 多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 23:46