[R] Adjusting length of series

Petr PIKAL petr.pikal at precheza.cz
Thu Jun 28 18:20:14 CEST 2012


Hi

I use R for quite a long time and as I remember I did not use such assign 
paste i loop yet. Insted of such construct with polluting environment with 
plenty of objects named something(i)somethingelse it is always advisable 
to use lists.

When you want to shorten variables to some common length (by cutting some 
portion of it) you can do it easily by:

lll<-list(a=1:10, b=1:9, c=1:8)
lll
$a
 [1]  1  2  3  4  5  6  7  8  9 10

$b
[1] 1 2 3 4 5 6 7 8 9

$c
[1] 1 2 3 4 5 6 7 8

shortest variable in lll
min(sapply(lll,length))
sapply(lll,"[",1:min( min(sapply(lll,length))))
     a b c
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
[4,] 4 4 4
[5,] 5 5 5
[6,] 6 6 6
[7,] 7 7 7
[8,] 8 8 8

Regards
Petr


> 
> Dear R Users,
>  
> I ask the following question in order to learn more on the use of 
'assign'
> and 'paste' functions and for loop; otherwise what I am asking could be 
> solved by binding the various first differences of the series using the 
> 'ts.union' operator.
>  
> The problem is:
> I have several variables in my dataset, which I should model dynamically 
-
> i.e., with lags of differences of the time series in the regression 
> equation. Consequently, I used a loop (on which I got help from Sarah 
> Goslee) to difference them.
>  
> Using the same variable as in my previous post, the first differences 
are 
> computed as follows:
>  
> > DCred1 <- diff(Cred, difference=1)                     #call this the 
FIRST LOOP
> > for(i in 2:5){
> +   print(assign(paste("DCred", i, sep=""), diff(get(paste("DCred", i-1,
> + sep="")), difference=1)))
> + }
> 
> NB:  I converted the series to time series using 'ts' before 
differencing.
>  
> Now after obtaining first differences, I try to use the 'assign' and 
> 'paste' function in two 'for loops' to adjust the lengths of lagged 
terms 
> (DCred1, DCred2, etc) to have the same length to be used in a regression 

> model. My code is:
>  
> for(i in 1:3){                                           #call this the 
SECOND LOOP
> for(j in 3:1){
> print(assign(paste("Dcre", i, sep=""), get(paste("DCred", i, 
sep=""))[j:(136-i)]))
> }
> }
>  
> NB: The length of the original series Cred (before differencing) equals 
> 136. This is why the last term in the assign expression is (136 - i).
> NB: I run this loop after running the first loop which computes the 
first 
> differences, so that the 'get' operator obtains DCred1, DCred2, etc from 

> the results of the first loop.
>  
> With this code, I expected to get DCred1 (whose length is 135) and 
> adjust its length to equal that of DCred3 (which is 133) and call the 
> result 'Dcre1'. Similarly, I intended to get DCred2 (whose length is 
134) 
> and adjust its length to 133 (the same as the length of DCred3) and call 

> it Dcre2. Lastly, I would get DCred3 of length 133 and call it Dcre3, 
with
> length 133. When I run this code, it runs succesfully. However, when I 
> then check the lengths of Dcre1, ..., Dcre3, I get:
>  
> > length(Dcre1)
> [1] 135
> > length(Dcre2)
> [1] 134
> > length(Dcre3)
> [1] 133
> 
> This shows that my code did NOT achieve the intended outcome.
> Please assist. Thanks.
>  
> Lexi
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list