有条件地计入dplyr

时间:2015-04-27 20:06:06

标签: r dplyr

我有一些会员订单数据,我想按订单周汇总。

这就是数据的样子:

MemberLTV <- memberorders %>%
group_by(MemID) %>%
summarize(
sum2 = sum(value[week<=2]),
sum4 = sum(value[week<=4]))

我使用dplyr进行group_by&#34; MemID&#34;并总结&#34;价值&#34;为&#34;周&#34; &lt; = 2和&lt; = 4(看看每个成员在第1-2周和第1-4周订购了多少。我目前的代码是:

output  = data.frame(MemID = c('A','B','C','D'),
                 sum2 = c(30,10,5,3),
                 sum4 = c(30,20,35,3),
                 count2 = c(2,1,1,1),
                 count4 = c(2,2,2,1))

我现在试图在汇总中添加另外两个字段,count2和count4,它们将计算每个条件的实例数(周<= 2且周<= 4)。

所需的输出是:

  $sql = "INSERT INTO `".$route."`(latitude, longitude, created_at)
          VALUES( ? , ? , NOW() )
          ON DUPLICATE KEY 
          UPDATE latitude = VALUES(latitude)
               , longitude = VALUES(longitude)";

  #var_dump($sql); # for debugging
  $stmt = $con->prepare($sql) or die ( $con->error );
  $stmt->bind_param("ss",$latitude,$longitude);
  $stmt->execute();

  $stmt->close();

我猜测它只是对sum函数进行了一些调整,但我很难搞清楚它。

3 个答案:

答案 0 :(得分:43)

尝试

 library(dplyr)
 memberorders %>% 
        group_by(MemID) %>% 
        summarise(sum2= sum(value[week<=2]), sum4= sum(value[week <=4]), 
                  count2=sum(week<=2), count4= sum(week<=4))

答案 1 :(得分:9)

使用预览创意并保持一致:

MemberLTV_2 <- memberorders %>%

group_by(MemID) %>%
summarize(

    count2 = length(value[week<=2]),
    count4 = length(value[week<=4]),
    sum2 = sum(value[week<=2]),
    sum4 = sum(value[week<=4])

    )

答案 2 :(得分:3)

使用plyr包可以做

ddply(memberorders,.(MemID),
                    summarise, 
                    val1 = sum(value[week<=2]), 
                    val2 = sum(value[week<=4]),
                    val3 = length(value[week<=2]),
                    val4 = length(value[week<=4]))

  MemID val1 val2 val3 val4
1     A   30   30    2    2
2     B   10   20    1    2
3     C    5   35    1    2
4     D    3    3    1    1