I have an array called "stocks" which contains numeric dates, ticker symbols,prices, etc.> stocks[1:3,]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.006377 However when I call the part of the array dealing with the Ticker, I get:> stocks[2,2][1] EWST 16185 Levels: A AA AAA AAAB AAABB AAB AABC AAC AACB AACC AACE AACI ... ZVXI Thus, when I reference this later, I get a numeric result and not the symbol? Is this because I need to change the format of stocks[,2] or because it has these extra "levels". For example: # START FIRST PORTFOLIO port1=matrix(0, ctr,3)#matrix for portfolio starting in June 30th 1995 c=0 # sort data at time t=1 (June 30th, 1995) for (i in 1:T) { if (stocks[i,1]==19950630) { c=c+1 row=i price = abs(stocks[i,8]) port1[c,1]=stocks[i,1] #put date in first column port1[c,2]=stocks[i,2] #ticker port1[c,3]=price #price } } WHEN I TRY> port1[1,2][1] 4934 I SHOULD GET [1] EWST ???????? [[alternative HTML version deleted]]
If you do an 'str' on your boject, you will probably find that TICKER is a factor. This is probably not what you want. So when you create the dataframe 'stock', use the parameter 'stringsAsFactors=FALSE' to prevent the conversion to factors. In the mean time, you can do: stocks$TICKER <- as.character(stocks$TICKER) On Tue, Aug 3, 2010 at 11:34 AM, Leigh E. Lommen <leigh.lommen at courtesycorporation.com> wrote:> I have an array called "stocks" which contains numeric dates, ticker > symbols,prices, etc. > > > >> stocks[1:3,] > > ? ? ?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.006377 > > > > However when I call the part of the array dealing with the Ticker, I > get: > > > >> stocks[2,2] > > [1] EWST > > 16185 Levels: ?A AA AAA AAAB AAABB AAB AABC AAC AACB AACC AACE AACI ... > ZVXI > > > > Thus, when I reference this later, I get a numeric result and not the > symbol? ?Is this because I need to change the format of stocks[,2] or > because it has these extra "levels". > > > > For example: > > > > # START FIRST PORTFOLIO > > port1=matrix(0, ctr,3)#matrix for portfolio starting in June 30th 1995 > > > > c=0 > > > > # sort data at time t=1 (June 30th, 1995) > > for (i in 1:T) > > ? ? ? ? ? ?{ > > ? ? ? ? ? ? ?if (stocks[i,1]==19950630) > > ? ? ? ? ? ? ? ? ? ? ? ?{ > > ? ? ? ? ? ? ? ? ? ? ? ? ?c=c+1 > > ? ? ? ? ? ? ? ? ? ? ? ? ?row=i > > ? ? ? ? ? ? ? ? ? ? ? ? ?price = abs(stocks[i,8]) > > > > ? ? ? ? ? ? ? ? ? ? ? ? ?port1[c,1]=stocks[i,1] #put date in first > column > > ? ? ? ? ? ? ? ? ? ? ? ? ?port1[c,2]=stocks[i,2] #ticker > > ? ? ? ? ? ? ? ? ? ? ? ? ?port1[c,3]=price #price > > ? ? ? ? ? ? ? ? ? ? ? ?} > > > > ? ? ? ? ? ?} > > > > WHEN I TRY > >> port1[1,2] > > [1] 4934 > > > > I SHOULD GET > > ?[1] EWST > > ???????? > > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Another solution factor(port1[,2], labels=levels(stocks[,2])) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/mixing-strings-and-numeric-doubles-in-an-array-tp2312091p2312313.html Sent from the R help mailing list archive at Nabble.com.
Leigh E. Lommen
2010-Aug-04 13:46 UTC
[R] argument is of length 0 & condition has length>1
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
David Winsemius
2010-Aug-04 13:55 UTC
[R] argument is of length 0 & condition has length>1
On Aug 4, 2010, at 9:46 AM, Leigh E. Lommen 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]) )^ Missing operator?> { > 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.David Winsemius, MD West Hartford, CT