0

I'm trying to write a function which will subset a dataframe for me, according to different lengths of time (number of months); and create a list of new dataframes which are all slightly different subsets. I want to be able to apply this function to any data.

Here is an example of what I'm trying to do.

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)

When I run this code, I get a variety of error messages.

This is the link to my original question, which was helpfully answered (thanks Carl), Create a list of a list of dataframes, by subsetting a list of dataframes in R . This time I'm trying to write a function to do this, and I'm probably doing something stupid.

Any help much appreciated, thanks.

7
  • It would help if you expanded on "a variety of error messages". Commented Mar 9, 2017 at 1:44
  • Sorry about that. The latest error message is Error in .subset2(x, i, exact = exact) : attempt to select less than one element in integerOneIndex . Thanks for your help. Commented Mar 9, 2017 at 2:00
  • First, x[[y]] can only retrieve a single column of a data.frame, but you are passing a vector in y; perhaps you mean x[y,]? Second, y is 0-based, but vectors in R are 1-based, so you are likely not getting back what you want. Commented Mar 9, 2017 at 2:05
  • That worked perfectly, thanks very much. Commented Mar 9, 2017 at 2:12
  • Update: I'm now getting the error message In Ops.factor(left, right) : ‘<=’ not meaningful for factors . Any ideas? Thanks. Commented Mar 9, 2017 at 2:29

1 Answer 1

0

Using split as suggested by 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.