将函数应用于r

时间:2018-03-05 02:59:11

标签: r function dplyr

为了简化,比方说,我有一个这样的数据集:

num = c(1,2,3,"NA",3,4,1,2,1)
char = c('a','b','s','s','s','s','a','s','s')
t = as.data.frame(cbind(num,char))   

我编写了一个函数来查找每列的前5个值:

 func_top5 = function(x){t%>%
    filter(!is.na(x))%>%
    group_by(x)%>%
    summarise(number_of_same_value = n())%>%
    arrange(desc(number_of_same_value))%>%
    slice(1:5)}

当我尝试将此功能应用于df时,

apply(t,2,func_top5)

它返回错误:

grouped_df_impl(data,unname(vars),drop)出错:   列x未知

但是当我单独使用该功能时,它完全正常:

t%>%
  filter(!is.na(num))%>%
  group_by(num)%>%
  summarise(number_of_same_value = n())%>%
  arrange(desc(number_of_same_value))%>%
  slice(1:5)

# A tibble: 5 x 2
     num number_of_same_value
  <fctr>                <int>
1      1                    3
2      2                    2
3      3                    2
4      4                    1
5     NA                    1

我认为问题可能是“group_by”功能。

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我们可以使用quosure方法来解决这个问题。假设他们输入参数'x'没有引用,我们可以使用enquo将其转换为quosure,然后使用bang-bang运算符({{1})在group_byfilter内进行评估})。请注意,最好将数据集对象也作为函数可用性的输入参数以更一般的方式使用。目前尚不清楚是否引用了缺失值。如果它是真正的NA,则更可接受的方式是!!

is.na

我们称之为

func_top5 <- function(df, x){
   x <- enquo(x)
   df %>%
       filter(! (!!(x) %in% c("NA", "")))%>%
        group_by(!! x)%>%
        summarise(number_of_same_value = n())%>%
        arrange(desc(number_of_same_value))%>%
        slice(1:5)
     }

在多列上执行此操作的一个选项是

func_top5(df1, col1)
# A tibble: 2 x 2
#   col1  number_of_same_value
#   <chr>                <int>
#1 b                        3
#2 a                        2

数据

map(names(t), ~ func_top5(t1, !! rlang::sym(.x)))
#[[1]]
# A tibble: 5 x 2
#    num number_of_same_value
#  <dbl>                <int>
#1  1.00                    3
#2  2.00                    2
#3  3.00                    2
#4  4.00                    1
#5 NA                       1

#[[2]]
# A tibble: 3 x 2
#  char  number_of_same_value
#  <chr>                <int>
#1 s                        6
#2 a                        2
#3 b                        1