Skip to content

remove loop which compares row i to row i-1

13 messages · Bert Gunter, David Winsemius, Joshua Wiley +3 more

#
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:

    for (i in 2:length(tUnitsort$Hour)){
      ifelse(tUnitsort[i,4]>=tUnitsort[i-1,4],(tempMC
=tUnitsort[i,7]),tempMC ) #col. 4 = BlockNumber; note tests to see if the
offers have change to the next set of blocks. 
      ifelse(tUnitsort[i,4]>=tUnitsort[i-1,4],(tempAC
=tUnitsort[i,7]-(tUnitsort[i,8]-tUnitsort[i,9])),tempAC )
      tUnitsort$MC[i] <- tempMC
      tUnitsort$AC[i] <- tempAC
      tUnitsort$PercentofMC[i] <- tUnitsort$Size[i]/tempMC
      tUnitsort$PercentofAC[i] <- tUnitsort$AvailableMW[i]/tempAC
    }

--
View this message in context: http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327.html
Sent from the R help mailing list archive at Nabble.com.
2 days later
#
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-row-i-1-tp4635327p4635566.html
Sent from the R help mailing list archive at Nabble.com.
#
On Jul 5, 2012, at 6:52 PM, jcrosbie wrote:

            
Presumably tempMC is a vector of the appropriate length, in which case  
this should repalce that loop:

tempMC [ diff(tUnitsort) > 0 ] <- tUnitsort[ , 7][  diff(tUnitsort) >  
0 ]
Doesn't work .... means what?
How can we tell without a worked example????
Dear tired Nabble user. Please read the Posting Guide.

        
>>>>>>>AND>>>>>>>>

  
    
#
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 vectors:

v1 >= v2

so try creating all four vectors and making sure they are what you
want and are the appropriate length.  Then:

results <- ifelse(v1 >= v2, VIT, VIF)

will work.

Cheers,

Josh
On Thu, Jul 5, 2012 at 3:52 PM, jcrosbie <james at crosb.ie> wrote:

  
    
6 days later
#
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

Date        Hour   BlockNumber  MyTo  NewColumn
2011-01   1          2                            140     140
2011-01   1          1                           70        140      
2011-01   1          0                           0           140   
2011-02   1          2                           160     160
2011-02   1          1                           70        160
2011-02   1          0                           0          160
2011-03   1          2                           150     150

I want to create a NewColumn which will place the MyTo number for the
highest block number for the rest blocks in a given hour ending within a
day. 


ifelse(tUnitsort[,4]>=tUnitsort[-1,4],tUnitsort[,7],tUnitsort[-1,7]) 

I am unsure how to refference the element before in this case.  I thought
the -1 was doing this but I believe I'm wrong now. 

http://r.789695.n4.nabble.com/file/n4636337/BR3_2011_New.csv
BR3_2011_New.csv 

--
View this message in context: http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636337.html
Sent from the R help mailing list archive at Nabble.com.
#
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)
         grep(x, names(tUnitsort)))

# This does it
# Use full data.frame 'tUnitsort' if you want
data.tmp <- aggregate(MyTo ~ Date + Hour, data = tUnitsort[, cols], max)
data.tmp <- merge(tUnitsort[, cols], data.tmp, by=c("Date", "Hour"))

# Make it pretty
idx <- grep("MyTo", names(data.tmp))
names(data.tmp)[idx] <- c("MyTo", "NewColumn")

# See it
head(data.tmp, 20)
tail(data.tmp, 20)


Also, you should quote the context. Many, almost all of us do NOT read 
the posts on Nabble. And Nabble does have a "quote" button.

Hope this helps,

Rui Barradas

Em 12-07-2012 18:55, jcrosbie escreveu:
#
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

Em 12-07-2012 22:43, jcrosbie escreveu:
3 days later
#
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 find create a new vector such that the sums of the 1's stops each
time there is a 0 and starts again next time there is a one. 
Output would be:
4,1, 5, 1,2,2,1,2,3



--
View this message in context: http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636662.html
Sent from the R help mailing list archive at Nabble.com.
#
On Jul 16, 2012, at 10:27 AM, jcrosbie wrote:

            
> x <- scan()
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
37: 1 1 0 0 0 0
43:
Read 42 items
 > sx <- tapply(x, cumsum(x==0), FUN= function(z) sum(z) )
 > sx[sx>0]
  0  1  3  6  8 12 15 16 17
  4  1  5  1  2  2  1  2  3
 > unname(sx[sx>0])
[1] 4 1 5 1 2 2 1 2 3
David Winsemius, MD
today: Springdale, UT