Mark Knecht
2009-Jul-12 18:53 UTC
[R] for (n in SystemResults$EnTime) return EnTime[n] until reaching "(all)"
Hi, Newbie alert on for loops... I have a bunch of data.frames built using rbind that have repeated values in the EnTime column. I want to read the value in the EnTime column and use it as an input to a function, but only down to the first occurrence of the string "(all)" where I want to break off and do other things. From the results I'm getting from some test code I clearly don't understand how this is supposed to work. Here's a picture of what's in the first column of a sample data.frame:> SystemResults$EnTime[1] 853 906 919 932 945 958 1011 1024 1037 1050 1103 1116 1129 1142 1155 1208 1221 1234 1247 1300 (all) 853 906 919 932 945 958 1011 1024 1037 1050 [32] 1103 1116 1129 1142 1155 1208 1221 1234 1247 1300 (all) Levels: 853 906 919 932 945 958 1011 1024 1037 1050 1103 1116 1129 1142 1155 1208 1221 1234 1247 1300 (all)> class(SystemResults$EnTime)[1] "factor"> mode(SystemResults$EnTime)[1] "numeric">As a test I tried to print down to the string "(all)" and then break but this code and everything I've tried so far is terribly wrong. Every attempt prints lots of error messages. I'm not grasping at all what I'm doing wrong or what's the right way to do this sort of thing. Clearly my first for loop isn't a success! for(n in SystemResults$EnTime) { if(SystemResults$EnTime[n] == "(all)") break) X = SystemResults$EnTime[n] print(X) } What I hoped to see was 853 906 919 932 945 ... 1234 1247 1300 My presumption was that I would eventually use X as the input to another function that does the work I want to do but this prints so much garbage that I'm clearly in the dark. Thanks in advance to anyone able to straighten me out. Cheers, Mark
Mark Knecht
2009-Jul-12 19:06 UTC
[R] for (n in SystemResults$EnTime) return EnTime[n] until reaching "(all)"
On Sun, Jul 12, 2009 at 11:53 AM, Mark Knecht<markknecht at gmail.com> wrote: <SNIP> So this gets better in terms of error messages but still has problems for(n in SystemResults$EnTime) { if ( SystemResults$EnTime[n] == "(all)") break else X SystemResults$EnTime[n] print(X) }> for(n in SystemResults$EnTime) {+ if ( SystemResults$EnTime[n] == "(all)") break else X SystemResults$EnTime[n] + print(X) + } Error in if (SystemResults$EnTime[n] == "(all)") break else X SystemResults$EnTime[n] : missing value where TRUE/FALSE needed>
David Winsemius
2009-Jul-12 19:35 UTC
[R] for (n in SystemResults$EnTime) return EnTime[n] until reaching "(all)"
On Jul 12, 2009, at 2:53 PM, Mark Knecht wrote:> Hi, > Newbie alert on for loops... > > I have a bunch of data.frames built using rbind that have repeated > values in the EnTime column. I want to read the value in the EnTime > column and use it as an input to a function, but only down to the > first occurrence of the string "(all)" where I want to break off and > do other things. From the results I'm getting from some test code I > clearly don't understand how this is supposed to work. > > Here's a picture of what's in the first column of a sample > data.frame: > >> SystemResults$EnTime > [1] 853 906 919 932 945 958 1011 1024 1037 1050 1103 > 1116 1129 1142 1155 1208 1221 1234 1247 1300 (all) 853 906 > 919 932 945 958 1011 1024 1037 1050 > [32] 1103 1116 1129 1142 1155 1208 1221 1234 1247 1300 (all) > Levels: 853 906 919 932 945 958 1011 1024 1037 1050 1103 1116 1129 > 1142 1155 1208 1221 1234 1247 1300 (all) >> class(SystemResults$EnTime) > [1] "factor"This is the big clue.>> mode(SystemResults$EnTime) > [1] "numeric" >> > > As a test I tried to print down to the string "(all)" and then > break but this code and everything I've tried so far is terribly > wrong. Every attempt prints lots of error messages. I'm not grasping > at all what I'm doing wrong or what's the right way to do this sort of > thing. Clearly my first for loop isn't a success! > > for(n in SystemResults$EnTime) { > if(SystemResults$EnTime[n] == "(all)") break)Inside the loop, shouldn't you be comparing to "n"?? As you have it now, the values of that factor are probably being used as indices to itself. (Not good.) Also not good is the use of "break". It looks to be fairly severely deprecated at this point> X = SystemResults$EnTime[n] > print(X) > }Another problem is that print() might return the internal integer representation of that factor after that assignment. Try something like: tf <- factor(c(53 , 906 , 919 , 932 , 945 , 958 , 1011 , 1024 , "(all)") ) for(n in tf ) {if (n != "(all)") print(n) } [1] "53" [1] "906" [1] "919" [1] "932" [1] "945" [1] "958" [1] "1011" [1] "1024" "The R-ight way might be to use one of the apply functions.)> > What I hoped to see was > 853 > 906 > 919 > 932 > 945 > ... > 1234 > 1247 > 1300 > > My presumption was that I would eventually use X as the input to > another function that does the work I want to do but this prints so > much garbage that I'm clearly in the dark.Why not use "n" if you are going to follow the loopy path. David Winsemius, MD Heritage Laboratories West Hartford, CT