Dear R-Users,

I have a dataframe ('forexdata') of daily returns from the foreign exchange market for three currencies - British Pound (bp), Canadian Dollar(cd),
Deustche Mark (dm) vis-a-vis the US Dollar and the Date Of
Trade(yymmdd).

For some dates the returns are missing (recorded as zero) as there
were no trades in that currency for that date. My task is to
substitute the missing or zero values with the next non-zero value.

To this end I created two lag variables:

forexdata$counter <- rep(1:length(forexdata$DateOfTrade), 1);   

forexdata$bplag1 <- forexdata$bp[forexdata$counter+1];            
forexdata$bplag2 <- forexdata$bp[forexdata$counter+2];      

forexdata$counter <- NULL;

> forexdata;
   DateOfTrade DayOfWeek     bp     cd      dm bplag1 bplag2
1       730606         3 2.5715 1.0011 0.37700 2.5740 0.0000
2       730607         4 2.5740 1.0017 0.37890 0.0000 0.0000
3       730608         5 0.0000 1.0015 0.38170 0.0000 2.5747
4       730611         1 0.0000 1.0019 0.38000 2.5747 2.5740
5       730612         2 2.5747 1.0018 0.38290 2.5740 0.0000
6       730613         3 2.5740 1.0005 0.38025 0.0000 2.5805
7       730614         4 0.0000 0.9998 0.38560 2.5805 2.5815
8       730615         5 2.5805 1.0001 0.38840 2.5815 2.5725
9       730618         1 2.5815 1.0003 0.39100 2.5725     NA
10      730619         2 2.5725 1.0003 0.38960     NA     NA


I have written a function which uses two lag variables for
substitution. The substitution seems to be happening alright within
the function. However, as a last step, I am unable able to replace
these values in the original data frame

> exch.replace <- function (df, curr, currlag1, currlag2) {
                    if  ((df[curr]==0) && (df[currlag1]!=0))  ( 
                        df[curr] <- df[currlag1]
                    )
                    else if  ((df[curr]==0) && (df[currlag1]==0)) ( 
                        df[curr] <- df[currlag1] <- df[currlag2]
                    )                                  
                    return(df[curr] );                
                  }   

> apply(forexdata, 1, exch.replace,
                      curr="bp", 
                      currlag1="bplag1", 
                      currlag2="bplag2");       

The result of applying this function is:

      1      2      3      4      5      6      7      8      9     10 
 2.5715 2.5740 2.5747 2.5747 2.5747 2.5740 2.5805 2.5805 2.5815 2.5725 
>

Any help in accomplishing this final step will be greatly appreciated.

a_rao		 


Find the best deals on the web at AltaVista Shopping!
http://www.shopping.altavista.com
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

