[R-SIG-Finance] Coercing LHS to a list

rex rex at nosyntax.net
Thu Sep 27 18:59:17 CEST 2012


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
[...]

sean fallon <sf99167 at yahoo.com> [2012-09-27 02:51]:
> 
>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.



More information about the R-SIG-Finance mailing list