Dear Rex,
 
Thanks for all your help on this. Much appreciated
 
I, in turn, also did more R&D on my first question of sorting the data in 
reverse and came up with this solution. Refer -> 
http://www.ats.ucla.edu/stat/r/faq/sort.htm
 
data1<-read.csv("http://ichart.finance.yahoo.com/table.csv?s=INFY.BO&a=08&b=16&c=2008&d=08&e=26&f=2012&g=d&ignore=.csv";)
attach(data1)
data1[1:10,]
sort1.data <- data1[order(Date), ]
sort1.data[1:15,]
data<-sort1.data
head(data)

#Add the SMA/LMA
h=data[,3]
c=data[,5]
hcdif=h-c
data$smv=filter(data[,5],rep(1/7,7), sides=1)
data$lmv=filter(data[,5],rep(1/21,21), sides=1)
data$HminusC=hcdif
write.csv(data,file="infyout.csv")

This also eliminates the Coercing LHS to a list error,

Am still taking baby steps in R but please let me know what you think of the 
above

Will now progress to explore more on the Quantmod library, thanks for pointing 
me in that direction.

regards

Sean
 
 
 

________________________________
 From: rex <[email protected]>
To: r-sig-finance <[email protected]> 
Sent: Thursday, September 27, 2012 10:29 PM
Subject: Re: [R-SIG-Finance] Coercing LHS to a list
  
For reference, here's the corrected code. Pasting it into a new R
session in a new directory should produce the results I think you 
want. Discussion of the rationale is below the code.

library(quantmod)
options(width=132)
getSymbols("INFY.BO", from="2008-01-01")
dat        = INFY.BO
dat$hminus = rep(0, NROW(dat))
dat$hminus = Hi(dat)-Cl(dat)
dat$smv    = rep(0, NROW(dat))
dat$smv    = filter(Cl(dat), rep(1/7,7), sides=1)
dat$lmv    = rep(0, NROW(dat))
dat$lmv    = filter(Cl(dat), rep(1/21,21), sides=1)
write.csv(dat, file="infyout.csv")
#you might want to save a zoo file instead. cf help(write.zoo)
head(dat,25)
             INFY.BO.Open INFY.BO.High INFY.BO.Low INFY.BO.Close INFY.BO.Volume 
INFY.BO.Adjusted hminus      smv      lmv
2008-01-01       1758.0      1777.15     1743.00       1749.70          51700   
       1639.38  27.45       NA       NA
2008-01-02       1780.0      1785.00     1715.35       1749.40          86700   
       1639.10  35.60       NA       NA
[...]


> 
>library(quantmod)
>getSymbols("INFY.BO", from="2008-01-01")
>data<-INFY.BO
>#Have assigned INFY.BO to data

I'd use dat <- INFY.BO because data is a
 function. 
  
>#Now to add the column for high-close
>hcdif = Hi(data)-Cl(data)
>data$hminus<-hcdif  #this works fine
>datasmv=filter(Cl(data),rep(1/7,7), sides=1) 
>data$smv<-datasmv #this also works fine
>data[30:40, ] #displays the data file properly
>---------------------------------------------------
> 
>  INFY.BO.Open INFY.BO.High INFY.BO.Low INFY.BO.Close INFY.BO.Volume
> [1,]       1560.0       1598.0     1518.00       1558.75         375700
> [2,]       1570.0       1600.0     1536.10       1545.40         183100
[...]
>      INFY.BO.Adjusted hminus      smv
> [1,]          1460.47  39.25 1563.800
> [2,]     
     1447.96  54.60 1557.271
[...]
>----------------------------------------------------------

You must have variables hanging around from earlier work. I suggest creating a 
new directory and starting R there. I say this because here's what INFO.BO
and dat look like here:

> INFY.BO[30:31]
              INFY.BO.Open INFY.BO.High INFY.BO.Low INFY.BO.Close 
INFY.BO.Volume INFY.BO.Adjusted
2008-02-11       1560.0       1598.0     1518.00       1558.75         375700   
       1460.47
2008-02-12       1570.0       1600.0     1536.10       1545.40         183100   
       1447.96
> dat[30:31]
       
       INFY.BO.Open INFY.BO.High INFY.BO.Low INFY.BO.Close INFY.BO.Volume 
INFY.BO.Adjusted
2008-02-11       1560.0       1598.0     1518.00       1558.75         375700   
       1460.47
2008-02-12       1570.0       1600.0     1536.10       1545.40         183100   
       1447.96


Note that rows are indexed by dates:

> dat['2008-01-02/2008-01-07']
             INFY.BO.Open INFY.BO.High INFY.BO.Low INFY.BO.Close INFY.BO.Volume 
INFY.BO.Adjusted
2008-01-02       1780.0         1785     1715.35        1749.4          86700   
       1639.10
2008-01-03      
 1740.0         1750     1691.60        1713.0         214300          1605.00
2008-01-04       1711.3         1730     1680.00        1694.8         134400   
       1587.94
2008-01-07       1670.0         1681     1623.00        1638.1         307500   
       1534.82

There are 6 columns (the date is not a column, e.g., dat[,1] is the
open, not the date), and there are no hminus and smv columns. Since
there are no such columns, assignments to them will fail. You need to
create the desired columns before you can assign to them.

dat = INFY.BO
dat$hminus = rep(0, NROW(data))
dat$smv = rep(0, NROW(data))
dat$lmv
 = rep(0, NROW(data))

head(dat) will show you the new columns have been added with values of 0.

>write.csv(data,file="infyout.csv") #writes to CSV for future use.

If you want to save the added columns, the write is premature. Add the
columns you want and then save the file.
> 
>However, if I want to add the lmv, and if I place these two lines of code below
>"data[30:40, ] #displays the data file properly", then I face the problem
> 
>datalmv=filter(Cl(data),rep(1/21,21), sides=1) #this calculation is fine
>data$lmv<-datalmv

Now that the column exists, write to it in one line:

dat$lmv=filter(Cl(data),rep(1/21,21), sides=1)
  
>Warning message:
>In data$lmv <- datalmv : Coercing LHS to a list
> 
>This is where I am stuck.

There was no data$lmv column. Now there is.

HTH,

-rex
-- 
Computers run on smoke. If it leaks
 out, they stop running.

_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should 
go.
        [[alternative HTML version deleted]]

_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should 
go.

Reply via email to