[R] Newbie code to count runs of up or down moves
Marc Schwartz
marc_schwartz at me.com
Mon Mar 25 20:55:09 CET 2013
In full agreement with Sarah's comments.
That being said, the Magic 8 Ball has the following in the little window for the OP:
?rle
Regards,
Marc Schwartz
On Mar 25, 2013, at 2:44 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
> This is unreadable. Please repost in plain text, ideally NOT using Nabble,
> and please also tell us what you're trying to accomplish.
>
> Sarah
>
> On Monday, March 25, 2013, Newbie1234 wrote:
>
>> There are probably many mistakes with this code. I am used to coding in C
>> with a debugger, so I am very new to coding in R without one and different
>> syntax. My code in the upper left panel of R Studio
>>
>> isz<-c(3,1,4,5,2,1,0,3,5,8)zlength(z)y<-c(0,0,0,0,0,0,0,0,0,0)ylength(y)zdiff
>> = diff(z)zdiffn<-length(zdiff)nx<-zdifff<-function(x) {
>> for (k in 1:n){ for (i in 1:n-k){ if
>> (all(x[i] * x[i-1] > 0) && ((x[i+k+1] * x[i+k]) < 0)
>> y[k]<-y[k]+1 } }
>> return(y)}f(x)__________I get back > z [1] 3 1 4 5 2 1 0 3 5 8>
>> length(z)[1]
>> 10> y<-c(0,0,0,0,0,0,0,0,0,0)> y [1] 0 0 0 0 0 0 0 0 0 0> length(y)[1] 10>
>> zdiff = diff(z)> zdiff[1] -2 3 1 -3 -1 -1 3 2 3> n<-length(zdiff)>
>> n[1]
>> 9> x<-zdiff> > f<-function(x) + {+ for (k in 1:n){+
>> for (i in 1:n-k){+ if (all(x[i] * x[i-1] > 0) &&
>> ((x[i+k+1] * x[i+k]) < 0)+ y[k]<-y[k]+1 Error:
>> unexpected symbol in:" if (all(x[i] * x[i-1] > 0) &&
>> ((x[i+k+1] * x[i+k]) < 0) y"> }Error:
>> unexpected '}' in " }"> > }Error:
>> unexpected '}' in " }"> return(y)Error: no function to
>> return from, jumping to top level> }Error: unexpected '}' in "}"> f(x)Error
>> in if (all(x[i] * x[i - 1] > 0) && ((x[i + k + 1] * x[i + k]) < :
>> missing
>> value where TRUE/FALSE needed_________Perhaps there is someway to use NULL
>> or NA?
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Newbie-code-to-count-runs-of-up-or-down-moves-tp4662423.html
>> Sent from the R help mailing list archive at Nabble.com.
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org <javascript:;> 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.
>>
>
>
> --
> Sarah Goslee
> http://www.stringpage.com
> http://www.sarahgoslee.com
> http://www.functionaldiversity.org
>
> [[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.
More information about the R-help
mailing list