我有以下形式的數據集:爆炸的日期範圍內的行爲R
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)
}
個人而言,我不喜歡,我需要在此基礎上轉換的情況下,這種變化在格蘭未來隱蔽的日期從列表中背部和供應原點的方式,但我真的很感激的想法,並幫助
所以有人不轉貼你已經做什麼,你可以編輯你的帖子,包括當前的方法?你提到「我的方法是使用各種各樣的樂器功能來做到這一點」。 – A5C1D2H2I1M1N2O1R2T1