使用if()在dplyr管道链中使用select()

时间:2017-06-22 09:01:47

标签: r dplyr

已阅读这两篇文章:

我使用Shiny input $ selector,如果用户选择了特定值,我希望我的数据帧与其他方式不同。

这是一个链条:

  filtered_funnel <-  reactive({
    lastmonth_funnel %>%
      filter(input$channel == "All" | Channel == input$channel) %>%
      filter(input$promo == "All" | Promo == input$promo) %>%

      ## HERE IS WHERE I'M STRUGGLING
      {if(input$promo != "none") select(., c("Channel", "Promo", "ShippingDetails", "Checkout", "Transactions"))} %>%


      gather(Funnel, Sessions, -Channel, -Promo) %>%
      group_by(Channel, Promo, Funnel) %>%
      summarise(Sessions = sum(Sessions))
  })

如果用户输入不等于&#34;无&#34;我想选择变量&#34;频道&#34;,&#34;促销&#34;,&#34; ShippingDetails&#34;,&#34; Checkout&#34;和&#34;交易&#34;。

我尝试了上述问题行的一些变体,但仍然出现错误:

当我在管道链中尝试这个时

{if(input$promo != "none") select(., c("Channel", "Promo", "ShippingDetails", "Checkout", "Transactions"))} %>% 

我收到了这个错误:

  

警告:错误:所有select()输入必须解析为整数列   位置。以下不:   * c(&#34;频道&#34;,&#34; Promo&#34;,&#34; ShippingDetails&#34;,&#34; Checkout&#34;,&#34; Transactions&#34;)

我也尝试过:

{if(input$promo != "none") select(., c(Channel, Promo, ShippingDetails, Checkout:Transactions))} %>%

这实际上一直运行直到我选择&#34;无&#34;在输入中,在这种情况下,我得到

  

错误:is.character(x)不是TRUE

我尝试这个时遇到了同样的错误:

{ifelse(input$promo != "none", select(., c(Channel, Promo, ShippingDetails, Checkout:Transactions)), .)} %>%

如何在dplyr管道中嵌入一个select语句,该语句表示输入$ promo!=&#34; none&#34;然后选择Channel,Promo,ShippingDetails,Checkout:来自管道中传递对象的事务?

- 这是随机生成数据的输入 -

> dput(lastmonth_funnel)
structure(list(Channel = c("Facebook", "Youtube", "SEM", "Organic", 
"Direct", "Email", "Facebook", "Youtube", "SEM", "Organic", "Direct", 
"Email", "Facebook", "Youtube", "SEM", "Organic", "Direct", "Email", 
"Facebook", "Youtube", "SEM", "Organic", "Direct", "Email", "Facebook", 
"Youtube", "SEM", "Organic", "Direct", "Email"), Promo = c("none", 
"none", "none", "none", "none", "none", "banannas", "banannas", 
"banannas", "banannas", "banannas", "banannas", "carrots", "carrots", 
"carrots", "carrots", "carrots", "carrots", "pears", "pears", 
"pears", "pears", "pears", "pears", "apples", "apples", "apples", 
"apples", "apples", "apples"), Sessions = c(6587, 3015, 6316, 
11219, 8117, 6473, 12464, 14032, 14318, 17535, 16219, 7838, 10685, 
12040, 19907, 13694, 6187, 16784, 21425, 18890, 24891, 16251, 
16977, 25206, 28573, 18704, 29178, 22069, 39687, 53734), AddToCart = c(279, 
4955, 5636, 8991, 15530, 18374, 9431, 5980, 4852, 5412, 4114, 
1782, 370, 3208, 6311, 9760, 7428, 6792, 3500, 5446, 1507, 783, 
2032, 833, 397, 2760, 5784, 9810, 13274, 14470), Registrations = c(194, 
3210, 3573, 6067, 10305, 12653, 6564, 3874, 3076, 3652, 2730, 
1227, 257, 2078, 4001, 6586, 4929, 4677, 2436, 3528, 955, 528, 
1348, 573, 276, 1788, 3667, 6620, 8808, 9964), ShippingDetails = c(134, 
2235, 2593, 4266, 7408, 9244, 4557, 2698, 2232, 2568, 1962, 896, 
178, 1447, 2904, 4631, 3543, 3417, 1691, 2457, 693, 371, 969, 
418, 191, 1245, 2661, 4655, 6332, 7280), Checkout = c(90, 1436, 
1792, 2864, 4672, 5666, 3078, 1734, 1543, 1724, 1237, 549, 120, 
930, 2007, 3109, 2234, 2094, 1142, 1579, 479, 249, 611, 256, 
129, 800, 1839, 3125, 3993, 4462), Transactions = c(59, 937, 
1192, 1819, 2602, 2926, 2039, 1132, 1026, 1095, 689, 283, 79, 
607, 1335, 1975, 1244, 1081, 756, 1031, 318, 158, 340, 132, 85, 
522, 1223, 1985, 2224, 2304)), class = "data.frame", row.names = c(NA, 
-30L), .Names = c("Channel", "Promo", "Sessions", "AddToCart", 
"Registrations", "ShippingDetails", "Checkout", "Transactions"
))

1 个答案:

答案 0 :(得分:3)

您需要确保{之间的语句返回data.frame而不管条件如何。所以你需要一个else .

cond <- FALSE

mtcars %>% 
  group_by(cyl) %>% 
  { if(cond) filter(., am == 1) else . } %>% 
  summarise(m = mean(wt))

TRUEFALSE合作正常。

(另请注意,像这样的简单示例确实使问题更容易掌握。)