[R] remove loop which compares row i to row i-1
Rui Barradas
ruipbarradas at sapo.pt
Thu Jul 12 23:11:08 CEST 2012
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.
>
> ______________________________________________
> 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