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

Mark Knecht markknecht at gmail.com
Tue Jul 7 21:06:59 CEST 2009


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.

   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
>




More information about the R-help mailing list