过滤给定数据并计算它们在r中的总和

时间:2018-07-25 15:36:19

标签: r

仅当客户在2018年1月7日更新并在2018年7月2日续约时,才筛选CUSTOMER_NO,如果此条件匹配,则将其相应的金额相加

       DATE CUSTOMER_NO  SUB_PACK  RESULT AMOUNT
1: 01-07-2018   987456321   SUB STF     sub    1.0
2: 02-07-2018   987456321   SUB STF renewal    1.0
3: 01-07-2018   963852741   SUB URU     sub    0.5
4: 02-07-2018   951263847   SUB URU renewal    1.0
5: 01-07-2018   789654123 SUB TUTOR     sub    0.5
6: 02-07-2018   789654123 SUB TUTOR renewal    1.0
7: 02-07-2018   965231487   SUB BAK renewal    5.0
8: 02-07-2018   859632147   SUB PAK renewal    3.0

pls可以帮助我,并为该查询提供确切的解决方案。.谢谢您

1 个答案:

答案 0 :(得分:0)

基本R:

 public void updateEvents() {
    weekView.setMonthChangeListener(new MonthLoader.MonthChangeListener() {
        @Override
        public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {

            Thread.currentThread().interrupt();

            APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
            Call<Schedule[]> call = apiInterface.getScheduleByUserID("5b56f3c714e86f36e0433359");
            call.enqueue(new Callback<Schedule[]>() {
                @Override
                public void onResponse(Call<Schedule[]> call, final Response<Schedule[]> response) {

                    start = response.body()[0].getStartDate();
                    end = response.body()[0].getStartDate();
                    Toast.makeText(DiaHorarioActivity.this, start.toString(), Toast.LENGTH_SHORT).show();
                    Toast.makeText(DiaHorarioActivity.this, end.toString(), Toast.LENGTH_SHORT).show();


                   Calendar startTime = new GregorianCalendar();
                    startTime.setTime(start);
                    Calendar endTime = (Calendar) startTime.clone();
                    endTime.setTime(end);
                    WeekViewEvent event = new WeekViewEvent(1, "test", startTime, endTime);
                    event.setColor(getResources().getColor(R.color.cardRejeitar));
                    events.add(event);


                }

                @Override
                public void onFailure(Call<Schedule[]> call, Throwable t) {
                    Toast.makeText(DiaHorarioActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                }
            });


           Thread.currentThread().run();


            return events;
        }
    });
}

数据表:

df <- data.frame(
    date   = c("01-07-2018", "02-07-2018", "01-07-2018", "02-07-2018", "01-07-2018", "02-07-2018", "02-07-2018", "02-07-2018"), 
    custno = c(987456321, 987456321, 963852741, 951263847, 789654123, 789654123, 965231487, 859632147),
    result = c("sub", "renewal", "sub", "renewal", "sub", "renewal", "renewal", "renewal"),
    amount = c(1, 1, 0.5, 1, 0.5, 1, 5, 3)
)

subs   <- df[df$date == "01-07-2018" & df$result == "sub",     "custno"]
renews <- df[df$date == "02-07-2018" & df$result == "renewal", "custno"]
important <- subs[subs %in% renews]
sum(df[df$custno %in% important, "amount"])