R将单周行转换为7天行

时间:2016-01-15 22:20:24

标签: r

今天早上我在网上看到了一些类似的例子和帖子,但我找不到任何与我的具体案例相似的内容。我的数据框看起来像这样:

    Week       Income   Views   Partner
    09/07/15   2000     345     Bob
    09/07/15   460      11980   Jane
    08/31/15   304      678     Mark

我需要的是在一周内每天复制的每个“周行”。您不必担心如何更改其他变量以便将它们除以7,我可以在没有帮助的情况下执行这些操作。我遇到的问题是将一周的行数扩展为总共7行,描述一周内的每一天,如下所示:

    Week       Income   Views   Partner
    09/07/15   2000     345     Bob
    09/08/15   2000     345     Bob
    09/09/15   2000     345     Bob
    09/10/15   2000     345     Bob
    09/11/15   2000     345     Bob
    09/12/15   2000     345     Bob
    09/13/15   2000     345     Bob
    09/07/15   460      11980   Jane
    09/08/15   460      11980   Jane
    09/09/15   460      11980   Jane
    09/10/15   460      11980   Jane
    09/11/15   460      11980   Jane
    09/12/15   460      11980   Jane
    09/13/15   460      11980   Jane
    08/31/15   304      678     Mark
    09/01/15   304      678     Mark
    09/02/15   304      678     Mark
    09/03/15   304      678     Mark
    09/04/15   304      678     Mark
    09/05/15   304      678     Mark
    09/06/15   304      678     Mark

就像我说的所有其他数据点都可以保留在NA(我可以轻松地使用na.locf)或只是将它们复制到列中。

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:4)

以下代码使用mapplyWeekPartner的每个组合创建新行。然后,我们将它包装在do.call(rbind, ...)中,将每个子组重新组合成一个数据帧。我在最终输出中留下原始的Week列仅用于插图,但可以删除。

# Sample data
dat = read.table(text="    Week       Income   Views   Partner
    09/07/2015   2000     345     Bob
    09/07/2015   460      11980   Jane
    08/31/2015   304      678     Mark"  , header=TRUE, stringsAsFactors=FALSE)
dat$Week = as.Date(dat$Week, "%m/%d/%Y")

newdat = do.call(rbind,
        mapply(function(w,p) {
          data.frame(Day=seq(w, w+6, 1), dat[dat$Week==w & dat$Partner==p, ])
        }, dat$Week, dat$Partner, SIMPLIFY=FALSE))

newdat
          Day       Week Income Views Partner
1  2015-09-07 2015-09-07   2000   345     Bob
2  2015-09-08 2015-09-07   2000   345     Bob
3  2015-09-09 2015-09-07   2000   345     Bob
4  2015-09-10 2015-09-07   2000   345     Bob
5  2015-09-11 2015-09-07   2000   345     Bob
6  2015-09-12 2015-09-07   2000   345     Bob
7  2015-09-13 2015-09-07   2000   345     Bob
8  2015-09-07 2015-09-07    460 11980    Jane
9  2015-09-08 2015-09-07    460 11980    Jane
10 2015-09-09 2015-09-07    460 11980    Jane
11 2015-09-10 2015-09-07    460 11980    Jane
12 2015-09-11 2015-09-07    460 11980    Jane
13 2015-09-12 2015-09-07    460 11980    Jane
14 2015-09-13 2015-09-07    460 11980    Jane
15 2015-08-31 2015-08-31    304   678    Mark
16 2015-09-01 2015-08-31    304   678    Mark
17 2015-09-02 2015-08-31    304   678    Mark
18 2015-09-03 2015-08-31    304   678    Mark
19 2015-09-04 2015-08-31    304   678    Mark
20 2015-09-05 2015-08-31    304   678    Mark
21 2015-09-06 2015-08-31    304   678    Mark

答案 1 :(得分:4)

另一种解决方案:

inout

答案 2 :(得分:2)

扩展@ epi10的答案,我们可以选择使用以下data.table方法:

library(data.table)

dat1 <- setDT(dat)[rep(seq_len(nrow(dat)), each=7),][,Day:=seq(min(Week),min(Week)+7,1),by=Partner] 

# Yields
dat1
          Week Income Views Partner        Day
 1: 2015-09-07   2000   345     Bob 2015-09-07
 2: 2015-09-07   2000   345     Bob 2015-09-08
 3: 2015-09-07   2000   345     Bob 2015-09-09
 4: 2015-09-07   2000   345     Bob 2015-09-10
 5: 2015-09-07   2000   345     Bob 2015-09-11
 6: 2015-09-07   2000   345     Bob 2015-09-12
 7: 2015-09-07   2000   345     Bob 2015-09-13
 8: 2015-09-07    460 11980    Jane 2015-09-07
 9: 2015-09-07    460 11980    Jane 2015-09-08
10: 2015-09-07    460 11980    Jane 2015-09-09
11: 2015-09-07    460 11980    Jane 2015-09-10
12: 2015-09-07    460 11980    Jane 2015-09-11
13: 2015-09-07    460 11980    Jane 2015-09-12
14: 2015-09-07    460 11980    Jane 2015-09-13
15: 2015-08-31    304   678    Mark 2015-08-31
16: 2015-08-31    304   678    Mark 2015-09-01
17: 2015-08-31    304   678    Mark 2015-09-02
18: 2015-08-31    304   678    Mark 2015-09-03
19: 2015-08-31    304   678    Mark 2015-09-04
20: 2015-08-31    304   678    Mark 2015-09-05
21: 2015-08-31    304   678    Mark 2015-09-06
          Week Income Views Partner        Day