无法在函数内部使用group_by,table和gglot

时间:2017-03-27 03:28:19

标签: r ggplot2 functional-programming dplyr

我希望能够为数据集返回group_by summary,变量表和geom_boxplot

可重复的例子:

set.seed(100)
df <- data.frame(Groups = rep(c("Group1","Group2","Group3"),times = 3), 
                 AGroup = rep(c("GroupA","GroupB","GroupC"), each = 3),
                 Amount = sample(400:500, 9))

数据框的打印输出

   > df
      Groups AGroup Amount
    1 Group1 GroupA    450
    2 Group2 GroupA    404
    3 Group3 GroupA    474
    4 Group1 GroupB    421
    5 Group2 GroupB    445
    6 Group3 GroupB    439
    7 Group1 GroupC    418
    8 Group2 GroupC    446
    9 Group3 GroupC    448

返回下面声明的输出的函数:

fun1 <- function(var_name) {
  table <- table(df$var_name)
  summary_table <- df %>% group_by_(var_name) %>% summarise(mean(Amount))
  plot <- ggplot(df, aes(var_name, Amount)) + geom_boxplot()
  list(table, summary_table, plot) }

这是函数的输出

> fun1("Groups")
[[1]]
< table of extent 0 >

[[2]]
# A tibble: 3 × 2
  Groups `mean(Amount)`
  <fctr>          <dbl>
1 Group1       429.6667
2 Group2       431.6667
3 Group3       453.6667

[[3]]

reproducible example

某处我错了..我猜..请帮忙

1 个答案:

答案 0 :(得分:1)

实际上,我尝试了这个并且工作了......令人惊讶的是小事情可以解决复杂的问题.. :)

  check2 <- function(var) {
  colindex <- which(colnames(dm1)==var)
  variable <- dm1[,colindex] %>% unlist() %>% as.factor()
  table <- table(dm1[,colindex])
  summarise_amount <- dm1 %>% group_by_(.dots = var) %>% summarise(mean(AmountSpent))
  plot <- ggplot(dm1, aes(x = variable,y = AmountSpent)) + geom_boxplot()
  list(table, summarise_amount, plot)
}

这是输出:

> with(dm1, check2("OwnHome"))
[[1]]

 Own Rent 
 516  484 

[[2]]
# A tibble: 2 × 2
  OwnHome `mean(AmountSpent)`
   <fctr>               <dbl>
1     Own           1543.1357
2    Rent            868.8264

[[3]]

Boxplot