2

我有一个表格的数据集:

df <- data.frame(var1 = c("1976-07-04" , "1980-07-04" , "1984-07-04" ), 
                   var2 = c('d', 'e', 'f'), 
                   freq = 1:3)

我可以通过以下方式使用索引非常快速地扩展此 data.frame:

df.expanded <- df[rep(seq_len(nrow(df)), df$freq), ]

但是,我想在日期上创建一个序列而不是复制,并让频率告诉我这个的长度。即对于第 3 行,我可以创建条目以填充爆炸的 data.frame:

seq(as.Date('1984-7-4'), by = 'days', length = 3)

谁能建议一种快速的方法来做到这一点?我的方法是使用各种 lapply 函数来做到这一点

我结合了 Gavin Simpson 的答案和以前的想法来解决我的问题。

ExtendedSeq <- function(df, freq.col, date.col, period = 'month') {
  #' An R function to take a data fame that has a frequency col and explode the 
  #' the dataframe to have that number of rows and based on a sequence.
  #'  Args:
  #'   df: A data.frame to be exploded.
  #'   freq.col: A column variable indicating the number of replicates in the 
  #'             new dataset to make.
  #'   date.col: A column variable indicating the name or position of the date
  #'             variable.
  #'   period: The periodicity to apply to the date.

  # Replicate expanded data form
  df.expanded <- df[rep(seq_len(nrow(df)), df[[freq.col]]), ]

  DateExpand <- function(row, df.ex, freq, col.date, period) {
    #' An inner functions to explode a data set and build out days sequence
    #'  Args:
    #'    row: Each row of a data set 
    #'    df.ex: A data.frame, to expand
    #'    freq: Column indicating the number of replicates to make.
    #'    date: Column indicating the date variable
    #'  Output:
    #'    An exploded data set based on a sequence expansion of a date.
    times <- df.ex[row, freq]
    # period <- can edit in the future if row / data driven.
    date.ex <- seq(df.ex[row, col.date], by = "days", length = times)
    return(date.ex)
  }

dates <- lapply(seq_len(nrow(df)), 
                FUN = DateExpand, 
                df.ex = df,
                freq = freq.col,
                col.date = date.col,
                period = period)

df.expanded[[date.col]] <- as.Date(unlist(dates), origin = '1970-01-01')
row.names(df.expanded) <- NULL
return(df.expanded)
}

就我个人而言,我不喜欢我需要从列表中隐藏日期并根据此转换提供来源的方式,以防将来发生变化,但我非常感谢这些想法和帮助

4

3 回答 3

3

这是一种方法:

extendDF <- function(x) {
    foo <- function(i, z) {
        times <- z[i, "freq"]
        out <- data.frame(seq(z[i, 1], by = "days", length = times),
                          rep(z[i, 2], times),
                          rep(z[i, 3], times))
        names(out) <- names(z)
        out
    }
    out <- lapply(seq_len(nrow(x)), FUN = foo, z = x)
    do.call("rbind", out)
}

这将迭代索引1:nrow(df)(即 的行索引df),将内联函数foo应用于 的每一行dffoo()本质上只是 extendsvar2和多次,并使用您对 extendsfreq的调用。该函数对列顺序、名称等进行了一些假设,但您可以根据需要进行修改。freqseq()var1

唯一的另一点是,将var1一个"Date"对象全部转换为一个对象而不是依次转换为每一行要高效得多extendDF(),因此首先进行一次转换,这里使用transform()

df <- transform(df, var1 = as.Date(var1))

然后打电话extendDF()

extendDF(df)

这给出了:

R> df <- transform(df, var1 = as.Date(var1))
R> extendDF(df)
        var1 var2 freq
1 1976-07-04    d    1
2 1980-07-04    e    2
3 1980-07-05    e    2
4 1984-07-04    f    3
5 1984-07-05    f    3
6 1984-07-06    f    3
于 2012-09-03T10:43:24.363 回答
1

短,不一定快:

library(plyr)
adply(df, 1, summarize, var3 = seq(as.Date(var1), by = "days", length = freq)) 
#         var1 var2 freq       var3
# 1 1976-07-04    d    1 1976-07-04
# 2 1980-07-04    e    2 1980-07-04
# 3 1980-07-04    e    2 1980-07-05
# 4 1984-07-04    f    3 1984-07-04
# 5 1984-07-04    f    3 1984-07-05
# 6 1984-07-04    f    3 1984-07-06
于 2012-09-03T10:54:38.893 回答
0

另一个:

df <- data.frame(var1 = c("1976-07-04" , "1980-07-04" , "1984-07-04" ),  var2 = c('d', 'e', 'f'),  freq = 1:3) 
df$id <- seq_len(nrow(df))
expanded <- apply(df[c("id","var1","freq")], MARGIN=1, FUN=function(x) {
  result <- seq.Date(as.Date(x["var1"]), length.out = as.integer(x["freq"]), by = "day")
  data.frame(id = rep(as.integer(x["id"]), length(result)), result=result)
})
expanded <- do.call(rbind, expanded)
expanded <- plyr:::join(x = expanded, y = df, by="id", type = "left", match = "first")
head(expanded)
于 2012-09-03T12:02:05.263 回答