Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建一个函数,该函数将通过在R中子集数据帧来生成数据帧列表_R_List_Dataframe_Subset_Lapply - Fatal编程技术网

创建一个函数,该函数将通过在R中子集数据帧来生成数据帧列表

创建一个函数,该函数将通过在R中子集数据帧来生成数据帧列表,r,list,dataframe,subset,lapply,R,List,Dataframe,Subset,Lapply,我正在尝试编写一个函数,它将根据不同的时间长度(月数)为我子集一个数据帧;并创建一个新数据帧列表,这些数据帧都是略有不同的子集。我希望能够将此函数应用于任何数据 这是一个我正在尝试做的例子 month <- c(0:35) product<- c(112:147) index <- rnorm(36) originaldata <- data.frame(month, product, index) sset <- function(df, time, leng

我正在尝试编写一个函数,它将根据不同的时间长度(月数)为我子集一个数据帧;并创建一个新数据帧列表,这些数据帧都是略有不同的子集。我希望能够将此函数应用于任何数据

这是一个我正在尝试做的例子

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)

月使用waterling建议的分割

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)

month如果您在“各种错误消息”上进行扩展,将会有所帮助。对此表示抱歉。最新的错误消息是.subset2(x,i,exact=exact)中的
错误:尝试在integerOneIndex中选择少于一个元素。感谢您的帮助。首先,
x[[y]]
只能检索data.frame的一列,但您正在
y
中传递向量;也许你的意思是
x[y,]
?其次,
y
是基于0的,但是R中的向量是基于1的,所以你可能无法得到你想要的。非常感谢。更新:我现在在Ops.factor(左、右)中得到错误消息