I missed something in my original reply: You intended a recursive approach, I think, but that wasn't clear in your code, and I simply repeated your actual code rather than your intended code in my response. If that's what you want, you need something more like:
DCred<- list() DCred[[1]] <- diff(Cred, difference=1) for(i in 2:5){ print(DCred[[i]]<- diff(DCred[[i-1]], lag=i, difference=1)) } Doing it with separately-named files will be much more complicated, something like DCred1 <- diff(Cred, difference=1) for(i in 2:5){ print(assign(paste("DCred", i, sep=""), diff(get(paste("DCred", i-1, sep="")), lag=i, difference=1)) } (both are untested) You might also benefit from reading Intro to R, which covers list structures. Sarah On Fri, Jun 15, 2012 at 9:22 AM, Lekgatlhamang, lexi Setlhare <lexisetlh...@yahoo.com> wrote: > Hi Sarah, > > Sorry for taking more of your time. I thought that before investigating the > use of assign and paste, I should first understand the approach of using > 'list()'. However, it seems to me that the use of list does not work very > well, unless I have wrongly implemented your suggestion. I implemented your > suggestion as follows: > DCred<- list() > for(i in 1:5){ > print(DCred[[i]]<- diff(DCred, lag=i, difference=1)) > } > Upon execution of the loop, the following was printed out: > list() > list() > list() > list() > list() > After the loop I tried to access the values for DCred1, DCred2, etc but to > no avail. See my commands and the associated output below: > >> DCred[[1]] > list() >> DCred[[i]] > list() >> DCred[1] > [[1]] > list() > Anyway, I then calculate DCred in order to see if the regression > > DCred <-lm(DCred~DCred1+DCred2) > > will work. > But it did not. Instead I got an error message - see below. > >> model<- lm(DCred~DCred[1]+DCred[2], drop.unused.levels = TRUE) > Error in model.frame.default(formula = DCred ~ DCred[1] + DCred[2], > drop.unused.levels = TRUE) : > variable lengths differ (found for 'DCred[1]') > > I then modified the right-hand-side of the equation, but still it did not > work: > >> model <- lm(DCred~DCred[[1]]) > Error in model.frame.default(formula = DCred ~ DCred[[1]], > drop.unused.levels = TRUE) : > variable lengths differ (found for 'DCred[[1]]') > > I will remain reading up on the assign and paste commands. > Thanks. Lexi > From: Sarah Goslee <sarah.gos...@gmail.com> > To: "Lekgatlhamang, lexi Setlhare" <lexisetlh...@yahoo.com> > Cc: "r-help@r-project.org" <r-help@r-project.org> > Sent: Friday, June 15, 2012 12:54 PM > Subject: Re: [R] Help with Sequential Differencing > > If you really want DCred1, DCred2, etc, you'll need to use assign() and > paste() within your loop. But he's a more R-ish way: > > # Differences of Cred > > DCred <- list() > for(i in 1:5){ > print(DCred[[i]]<- diff(DCred, lag=i, difference=1)) > > } > > DCred[[1]] > > Sarah > > On Friday, June 15, 2012, Lekgatlhamang, lexi Setlhare wrote: > > Dear R Users, > Sorry for what seems like I am re-posting. When I was typing my initial > posting, I intended to copy and paste the commands from my script, but ended > up forgetting. I am now pasting the commands in this email.NB: Below is a > copy of 'all' the relevant commands in my script > > ################################################## > data<- read.table("C:\\myfiles\\from drive D\\myfiles 080208\\An R Test > folder\\Dataset2a.csv", head=TRUE, sep=",") > str(data) > data > > # Extract the variables in data, rename and difference them > > Cred <- ts(data[, 2], frequency=12, start=c(2001, 1)) # Cred denotes Credit > > # Differences of Cred > DCred<- diff(Cred, difference=1) > DCred > for(i in 1:5){ > print(DCred[i]<- diff(DCred, lag=i, difference=1)) > > } > > DCred[1] > > DCred[2] > > DCred[3] > > ################################################ > Thanks Lexi > > > ________________________________ > > To: "r-help@r-project.org" <r-help@r-project.org> > Sent: Friday, June 15, 2012 11:39 AM > Subject: Help with Sequential Differencing > > > > > Dear R Users, > > I have struggled with the following problem for days, which I thought was > simple, although it would likely be basic to most of you. > I am working with time series data. > In my script, my intention is to create first differences of the variables > in the file so that I end up estimating an equation of the form: > DCred(t) =c + > DCred(t-1)+DCred(t-2)+...+DBoB(t)+DBoB(t-1)+DBoB(t-2)+...+Drvr(t)+Drvr(t-1)+Drvr(t-2)+...+e(t) > > Where D at the beginning of each variable represents 'change', for first > difference and e(t) is the error term. > Now I am trying to use loops to calculate 5 lagged first-differences of each > variable in the dataset - e.g., DCred(t-1), DCred(t-2), ..., DCred(t-5). > Example: > # Differences of Cred > DCred<- diff(Cred, difference=1) > DCred > for(i in 1:5){ > print(DCred[i]<- diff(DCred, lag=i, difference=1)) > } > After I calculated the contemporaneous first difference DCred, this loop is > meant to compute the subsequent first differences of the same variable; > i.e., DCred(t-1) and call it DCred1, DCred(t-2) and call it DCred2, ... and > DCred(t-5) and call it DCred5. > The loop works, at least I think so. But now after the loop has executed, > when I type DCred1[1] (which I thought would give me the first value in the > series for DCred(t-1)), called DCred1, I get a message "object 'DCred1' not > found". Similarly typing Dcred1[2] (which I thought would give the second > value of DCred(t-1)), ie., the second value of DCred1, gives "object > DCred1[2] not found", etc. > A copy of the commands and error messages is below: >> DCred1[1] > Error: object 'DCred1' not found >> DCred1[2] > Error: object 'DCred1' not found > > How can I solve this problem? Thank you kindly for your time. -- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.