按计数为重复项创建增量序列

时间:2019-03-06 19:34:42

标签: r sequence

我有一个数据框

template<class T> class Foo{
    T a;
    auto baz(){
        return 4.2f;
    }
};

/* First instantiated from: insights.cpp:9 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
class Foo<int>
{
  int a;
  inline auto baz();

  // inline Foo() noexcept = default;
  // inline constexpr Foo(const Foo<int> &) = default;
  // inline constexpr Foo(Foo<int> &&) = default;
};

#endif


int main()
{
  Foo<int> bar = Foo<int>();
  return 0;
}
Foo<int>::baz()

我正在使用以下方法按计数复制此数据帧:

 Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); 
 Mixer mixer = null;
 for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
   System.out.println(cnt + " " + mixerInfo[cnt].getName());
   mixer = AudioSystem.getMixer(mixerInfo[cnt]);
   Line.Info[] lineInfos = mixer.getTargetLineInfo();
    if (lineInfos.length >= 1 && lineInfos[0].getLineClass().equals(TargetDataLine.class)) {
      System.out.println(cnt + " Mic is supported!");
      // break;
    }
  }
 AudioFormat format = new AudioFormat(48000, 24, 2, true, true);
 DataLine.Info dataLineInfo = new DataLine.Info(
          TargetDataLine.class, format );
 mixer = AudioSystem.getMixer(mixerInfo[6]);

 targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
 targetDataLine.open(audioFormat);
 targetDataLine.start();
library(data.table)
dt <- data.table(Customer = c("a", "a", "c"), 
  count = c(3, 4, 5), 
  Date = c("2019-02-01","2019-05-01","2019-10-01"))

然后进一步使用 Customer count Date 1: a 3 2019-02-01 2: a 4 2019-05-01 3: c 5 2019-10-01 来增加月份字段

dt[rep(seq(1, nrow(dt)), dt$count)]
    Customer count       Date
 1:        a     3 2019-02-01
 2:        a     3 2019-02-01
 3:        a     3 2019-02-01
 4:        a     4 2019-05-01
 5:        a     4 2019-05-01
 6:        a     4 2019-05-01
 7:        a     4 2019-05-01
 8:        c     5 2019-10-01
 9:        c     5 2019-10-01
10:        c     5 2019-10-01
11:        c     5 2019-10-01
12:        c     5 2019-10-01

但是,第4行中的日期是第3行的增量(因为它是按客户分组的,在这种情况下,客户名称是相同的)。我正在寻找开始于“ 2019-05-01”的输出,这是针对原始数据框的原始日期。因此,我希望得到这样的东西:

months

1 个答案:

答案 0 :(得分:1)

我认为添加行号并对其进行分组应该有效:

dt %>% mutate(Date = as.Date(Date), rn = row_number()) %>% 
  slice(rep(row_number(), count)) %>%
  group_by(Customer, rn) %>%
  mutate(Date = seq(first(Date), by="3 months", length.out=n()))

# A tibble: 12 x 4
# Groups:   Customer, rn [3]
   Customer count Date          rn
   <chr>    <dbl> <date>     <int>
 1 a            3 2019-02-01     1
 2 a            3 2019-05-01     1
 3 a            3 2019-08-01     1
 4 a            4 2019-05-01     2
 5 a            4 2019-08-01     2
 6 a            4 2019-11-01     2
 7 a            4 2020-02-01     2
 8 c            5 2019-10-01     3
 9 c            5 2020-01-01     3
10 c            5 2020-04-01     3
11 c            5 2020-07-01     3
12 c            5 2020-10-01     3

首先,这是data.table类似物:

dt[, Date := as.IDate(Date)][, rn := .I]
res <- dt[rep(1:.N, count)][, 
  Date := seq(first(Date), by="3 months", length.out=.N)
, by=.(Customer, rn)][]

    Customer count       Date rn
 1:        a     3 2019-02-01  1
 2:        a     3 2019-05-01  1
 3:        a     3 2019-08-01  1
 4:        a     4 2019-05-01  2
 5:        a     4 2019-08-01  2
 6:        a     4 2019-11-01  2
 7:        a     4 2020-02-01  2
 8:        c     5 2019-10-01  3
 9:        c     5 2020-01-01  3
10:        c     5 2020-04-01  3
11:        c     5 2020-07-01  3
12:        c     5 2020-10-01  3

或更简洁(普通的dplyr动词不可能):

dt[, Date := as.IDate(Date)][, rn := .I]
dt[, 
  .(Customer, count, Date = seq(first(Date), length.out=count, by="3 months"))
, by=.(rn)]