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

Rui Barradas ruipbarradas at sapo.pt
Fri Jul 13 09:40:04 CEST 2012


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:
> 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] <ml-node+s789695n4636372h9 at n4.nabble.com>
> To: jcrosbie <james at crosb.ie>
> Sent: Thursday, July 12, 2012 3:12 PM
> Subject: Re: remove loop which compares row i to row i-1
>
>
> 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:
>
>> 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>>
> ______________________________________________
> [hidden email] 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.
>
>
> ________________________________
>
> If you reply to this email, your message will be added to the discussion below:
> http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636372.html
> To unsubscribe from remove loop which compares row i to row i-1, click here.
> NAML
>
> --
> View this message in context: http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636376.html
> Sent from the R help mailing list archive at Nabble.com.
> 	[[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