[英]Create a list of a list of dataframes, by subsetting a list of dataframes in R
[英]Create a function which will make a list of dataframes through subsetting a dataframe in R
我正在嘗試編寫一個函數,該函數將根據不同的時間長度(月數)為我分配一個數據幀; 並創建一個新數據框列表,這些數據框都是稍有不同的子集。 我希望能夠將此功能應用於任何數據。
這是我正在嘗試做的一個例子。
month <- c(0:35)
product<- c(112:147)
index <- rnorm(36)
originaldata <- data.frame(month, product, index)
sset <- function(df, time, length, windows) {
#Create the subset rule
subfun <- function(x,y,z) { x[x[[y]] >= z & x[[y]] <= z+length-1,] }
#Apply this rule to dataframe
regdfs <- lapply(1:windows,
function(j) {subfun(x = df, y = time, z = j - 1) })
}
#Apply sset function to create dataframe subsets
camsets <- sset(df = originaldata, time = originaldata$month, length = 13, windows = 24)
當我運行此代碼時,會收到各種錯誤消息。
這是我原始問題的鏈接,該問題得到了有幫助的回答(感謝Carl) ,通過在R中設置數據幀列表來創建數據幀列表 。 這次,我試圖編寫一個函數來執行此操作,並且我可能正在做一些愚蠢的事情。
任何幫助,不勝感激,謝謝。
根據澆水建議使用拆分
month <- c(0:35)
product <- c(112:147)
index <- rnorm(36)
originaldata <- data.frame(month, product, index)
createsubsets <- function(df, length, windows) {
cutoffs <-seq(0,windows*length,length)
originaldata$group <- cut(originaldata$month, cutoffs,include.lowest=TRUE )
split(originaldata, originaldata$group)
}
camsets <- createsubsets(df, 13, 24)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.