[R] Test for X=1 fails, test for >0 works, data in text file

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue Jul 7 21:28:46 CEST 2009


On 07-Jul-09 19:06:59, Mark Knecht wrote:
> Hi,
>    I am apparently not understanding some nuance about either the use
> of subset or more likely my ability to test for a numerical match
> using '='. Which is it? Thanks in advance.

It looks as though you have tripped over the distinction between "="
and "==". The first is (in effect) an assignment operator (like "<-").
The second is a comparison operator: "X==Y" is TRUE if X equals Y.

So, for example, to select rows from MyResults according to your
criteria below, you could do something like:

  MyResultsNeg <- MyResults[(MyResults$PosType==(-1)),]
  MyResultsPos <- MyResults[(MyResults$PosType==1   ),]

[Note: the parentheses "(...)" are in fact logically superfluous,
but I like to use them (a) to make it absolutely clear, visually,
how things are being evaluated; (b) (not relevant in this particular
context) to avoid falling into traps like "N <- 10 ; X <- 1:N-1"
which results in X == (0:9) = (1:10) - 1. The correct syntax for
the latter would be X <- 1:(N-1).]

Hoping this helps,
Ted.


>    I've read a data file, reshaped it and then created MyResults by
> keeping only lines where the value column is greater than 0. So far so
> good. The data in MyResults looks good to me by eye.
> 
>    The problem comes in when I try to further subset MyResults into
> two files, one with PosType=1 and the other with PosType=-1. Looking
> at the dimension of the results there's no change. It didn't work
> which is verified by eye. However if I test for PosType=1 by actually
> testing for PosType>0 then it does work.
> 
>    Is this the general case in R that if I've read data that was
> written into a csv file as 1 - it is 1 if I look into the file with a
> text editor - that I cannot test for that? Or is some case where I
> need to use maybe as.numeric or something else first to ensure R sees
> the number the way I'm thinking about the number?
> 
> Cheers,
> Mark
> 
>> dim(X)
> [1]  25 425
>>
>> ReShapeX <- melt(X, id = c("Trade", "PosType", "EnDate", "EnTime", 
>> "ExDate",  "ExTime", "PL_Pos", "Costs", "Save2"))
>>
>> dim(ReShapeX)
> [1] 10400    11
>>
>> MyResults <- subset(ReShapeX, value > 0)
>>
>> dim(MyResults)
> [1] 1105   11
>>
>> MyResults.GroupA <- subset(MyResults, PosType = 1)
>>
>> dim(MyResults.GroupA)
> [1] 1105   11
>>
>> MyResults.GroupB <- subset(MyResults, PosType = -1)
>>
>> dim(MyResults.GroupB)
> [1] 1105   11
>>
>> MyResults.GroupA <- subset(MyResults, PosType > 0)
>>
>> dim(MyResults.GroupA)
> [1] 432  11
>>
>> MyResults.GroupB <- subset(MyResults, PosType < 0)
>>
>> dim(MyResults.GroupB)
> [1] 673  11
>>
> 
> ______________________________________________
> 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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 07-Jul-09                                       Time: 20:28:44
------------------------------ XFMail ------------------------------




More information about the R-help mailing list