Re: [R] remove loop which compares row i to row i-1

2012-07-16 Thread David Winsemius
On Jul 16, 2012, at 10:27 AM, jcrosbie wrote: Thank you, That was very helpful. I do have another problem along the same lines. But I can not think of a way to do this with a function like ddply or aggregate. Example: x = sample(0:1,42,TRUE) [1] 1 1 1 1 0 1 0 0 1 1 1 1 1 0 0 0 1 0 0 1 1 0

Re: [R] remove loop which compares row i to row i-1

2012-07-16 Thread Jorge I Velez
Hi, Here is one way using rle(): > x = sample(0:1,42,TRUE) > rle(x) Run Length Encoding lengths: int [1:16] 2 2 2 3 1 1 2 1 3 3 ... values : int [1:16] 1 0 1 0 1 0 1 0 1 0 ... > x [1] 1 1 0 0 1 1 0 0 0 1 0 1 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 0 > r <- rle(x) > rle(x) R

Re: [R] remove loop which compares row i to row i-1

2012-07-16 Thread jcrosbie
Thank you, That was very helpful. I do have another problem along the same lines. But I can not think of a way to do this with a function like ddply or aggregate. Example: x = sample(0:1,42,TRUE) [1] 1 1 1 1 0 1 0 0 1 1 1 1 1 0 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 I want to

Re: [R] remove loop which compares row i to row i-1

2012-07-13 Thread Rui Barradas
Hello, Works unchanged with me. Yesterday it could have worked for some other reason, like having other variables in my environment, which I had, but this time I have started anew. Try including tUnitsmall <- tUnitsort[, cols] and then use this data.frame to see what happens. Rui Barradas

Re: [R] remove loop which compares row i to row i-1

2012-07-12 Thread jcrosbie
I'm sorry but I see to be getting an error.  > data.tmp <- aggregate(MyTo ~ Date + Hour, data = tUnitsort[, cols], max) > Error in `[.default`(xj, i) : invalid subscript type 'builtin' From: Rui Barradas [via R] To: jcrosbie Sent: Thursday, July 12, 2012

Re: [R] remove loop which compares row i to row i-1

2012-07-12 Thread Rui Barradas
Hello, I've not been following this thread but this seems ndependent from previous posts. Try the following. url <- "http://r.789695.n4.nabble.com/file/n4636337/BR3_2011_New.csv"; tUnitsort <- read.csv(url, header=TRUE) cols <- sapply(c("Date", "Hour", "BlockNumber", "MyTo"), function(x)

Re: [R] remove loop which compares row i to row i-1

2012-07-12 Thread jcrosbie
Thank you, I am sorry but I am still trying to figure out how to make the function work. I have a column called tUnitsort$BlockNumber which can range from 0 to 6. I have another two columns with the date and the hour ending for the given day. Example DateHour BlockNumber MyTo New

Re: [R] remove loop which compares row i to row i-1

2012-07-05 Thread Joshua Wiley
Hi, Great chance to practice debugging. Rather than trying one complicated statement, break it into pieces. The basic structure is: results <- ifelse(condition, value if true, value if false) Each argument needs to be a vector of the same length. In your case, condition itself consists of two

Re: [R] remove loop which compares row i to row i-1

2012-07-05 Thread David Winsemius
On Jul 5, 2012, at 6:52 PM, jcrosbie wrote: Thank you, I tired ifelse(tUnitsort[length(tUnitsort$Hour),4]>=tUnitsort[-1,4],(tempMC =tUnitsort[length(tUnitsort$Hour),7]),tempMC ) Presumably tempMC is a vector of the appropriate length, in which case this should repalce that loop: tempMC

Re: [R] remove loop which compares row i to row i-1

2012-07-05 Thread jcrosbie
Thank you, I tired ifelse(tUnitsort[length(tUnitsort$Hour),4]>=tUnitsort[-1,4],(tempMC =tUnitsort[length(tUnitsort$Hour),7]),tempMC ) But this doesn't seem to work. Where am I going wrong? -- View this message in context: http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-r

Re: [R] remove loop which compares row i to row i-1

2012-07-03 Thread Bert Gunter
Of course it _should_ be: ifelse(x[-length(x)] < x[-1], ...,...) Sorry... -- Bert On Tue, Jul 3, 2012 at 1:00 PM, Bert Gunter wrote: > Vectorize vectorize vectorize! > > if(x[-length(x)] < x[-1]) {...} > > (where x is the whole vector of entries) > > Bill Dunlap has posted some elegant code

Re: [R] remove loop which compares row i to row i-1

2012-07-03 Thread Bert Gunter
Vectorize vectorize vectorize! if(x[-length(x)] < x[-1]) {...} (where x is the whole vector of entries) Bill Dunlap has posted some elegant code within the last month or 2 aimed at this sort of thing, so search on his posts in the archive. -- Bert On Tue, Jul 3, 2012 at 12:10 PM, jcrosbie wro

[R] remove loop which compares row i to row i-1

2012-07-03 Thread jcrosbie
I would like to remove a loop to speed up my code. I want to remove a loop which references the last row. In general I want to a remove a loop which looks something like this: for 2 to number of rows in a matrix do{ if indextrow-1 is < currentIndexRow then do something. } My R code: fo