[R] argument is of length 0 & condition has length>1

Joshua Wiley jwiley.psych at gmail.com
Wed Aug 4 16:47:31 CEST 2010


Hi Leigh,

Several aspects of your email make it challenging to offer advice.  We
are missing part of your data (e.g., 'T' in 1:T), and it is not
terribly clear what 't' in (t-5) to t(19950630) to (t+7) is supposed
to mean (thought I would hazard the guess time, and that it is
represented by your 'DATE' column).

David pointed out that you are missing an operator, but I suspect your
loop has some additional problems.

My suggestion would be to break things down into smaller problems.
You have a number of conditionals, rather than deal with all at once,
try working with one at a time.  For instance:

for (i in 1:T) {
  if ((stocks[i, 1] == 19950630)) {
    cat("Delisted stocks are: \n", stocks[i, 2], fill = TRUE)
  }
}

for (i in 1:T) {
  if ( (stocks[i, 2] != stocks[i5, 2]) ) { #missing operator between i and 5?
    cat("Delisted stocks are: \n", stocks[i, 2], fill = TRUE)
  }
}

etc.

As a side note, I used rather than two separate print() lines, I broke
the line with \n.  Also, providing the results of:

dput(head(stocks))

rather than just head(stocks) would really make reading in your sample
data a lot easier.

Best regards,

Josh


On Wed, Aug 4, 2010 at 6:46 AM, Leigh E. Lommen
<leigh.lommen at courtesycorporation.com> wrote:
>
> I have the following array:
>
>> head(stocks)
>  DATE      TICKER PERMNO EXCHCD TSYMBOL TRDSTAT SHROUT    PRC       RET
>
> 1 19950131   EWST  10001      3    EWST       A   2224 -7.75000
> -0.031250
> 2 19950228   EWST  10001      3    EWST       A   2224  7.54688
> -0.026210
> 3 19950331   EWST  10001      3    EWST       A   2244  7.50000  0.00637
> 4 19950428   EWST  10001      3    EWST       A   2244  7.50000
> 0.000000
> 5 19950531   EWST  10001      3    EWST       A   2244 -7.87500
> 0.050000
> 6 19950630   EWST  10001      3    EWST       A   2254  8.25000
> 0.060317
>
> I am trying to find stocks(TICKER) that are delisted at points (t-5) to
> t(19950630) to (t+7) using the following code:
>
> #find delistings or incomplete returns
> for (i in 1:T)
>        {
>        if ( (stocks[i,1]==19950630)&
> (stocks[i,2]!=stocks[i5,2])|(stocks[i,2]!=stocks[i+5,2]) )
>                {
>                print("Delisted stocks are: ")
>                print(stocks[i,2])
>                }
>        }
>
> But I get the following error message:
>
> Error in if ((stocks[i, 1] == 19950630) & (stocks[i, 2] != stocks[i -  :
>
>  argument is of length zero
> In addition: Warning messages:
> 1: In if ((stocks[i, 1] == 19950630) & (stocks[i, 2] != stocks[i -  :
>  the condition has length > 1 and only the first element will be used
> 2: In if ((stocks[i, 1] == 19950630) & (stocks[i, 2] != stocks[i -  :
>  the condition has length > 1 and only the first element will be used
> 3: In if ((stocks[i, 1] == 19950630) & (stocks[i, 2] != stocks[i -  :
>  the condition has length > 1 and only the first element will be used
> 4: In if ((stocks[i, 1] == 19950630) & (stocks[i, 2] != stocks[i -  :
>  the condition has length > 1 and only the first element will be used
>
> Please help??
>
> Regards,
> Leigh
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list