Mark Knecht
2009-Jul-07 19:06 UTC
[R] Test for X=1 fails, test for >0 works, data in text file is 1
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>
Henrique Dallazuanna
2009-Jul-07 19:17 UTC
[R] Test for X=1 fails, test for >0 works, data in text file is 1
Try this: MyResults.GroupA <- subset(MyResults, PosType == 1) On Tue, Jul 7, 2009 at 4:06 PM, Mark Knecht <markknecht@gmail.com> 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. > > 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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Jorge Ivan Velez
2009-Jul-07 19:21 UTC
[R] Test for X=1 fails, test for >0 works, data in text file is 1
Hi Mark, X = 1 assings the number 1 to X whereas X == 1 test if X is equal to 1. I suspect you want to do this :-) Here is an example: # R code X = 1 X # [1] 1 X == 1 # [1] TRUE HTH, Jorge On Tue, Jul 7, 2009 at 3:06 PM, Mark Knecht <markknecht@gmail.com> 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. > > 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@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. >[[alternative HTML version deleted]]
Duncan Murdoch
2009-Jul-07 19:23 UTC
[R] Test for X=1 fails, test for >0 works, data in text file is 1
On 7/7/2009 3:06 PM, 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. > > 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. LookingPosType = 1 is an assignment. The test is PosType == 1. Duncan Murdoch> 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.
(Ted Harding)
2009-Jul-07 19:28 UTC
[R] Test for X=1 fails, test for >0 works, data in text file
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 ------------------------------