|
|
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
}
|
|
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 < [hidden email]> wrote:
>
> 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.
>
> ______________________________________________
> [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.
>
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
Of course it _should_ be:
ifelse(x[-length(x)] < x[-1], ...,...)
Sorry...
-- Bert
On Tue, Jul 3, 2012 at 1:00 PM, Bert Gunter < [hidden email]> 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 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 < [hidden email]> wrote:
>
>>
>> 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.
>>
>> ______________________________________________
>> [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.
>>
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
>
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm>
>
>
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
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?
|
|
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 [ diff(tUnitsort) > 0 ] <- tUnitsort[ , 7][ diff(tUnitsort) >
0 ]
>
> But this doesn't seem to work.
Doesn't work .... means what?
>
> Where am I going wrong?
How can we tell without a worked example????
>
> --
> 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.
Dear tired Nabble user. Please read the Posting Guide.
>
> >>>>>>>>>>>>>>>>>>>>>>>>
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >>>>>>>AND>>>>>>>>
> .....provide commented, minimal, self-contained, reproducible code.
--
David Winsemius, MD
West Hartford, CT
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
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 < [hidden email]> wrote:
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
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.
BR3_2011_New.csv
|
|
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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
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] <[hidden email]> To: jcrosbie <[hidden email]> 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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland 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
|
|
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] < [hidden email]>
> To: jcrosbie < [hidden email]>
> 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]]
>
>
>
> ______________________________________________
> [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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
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
|
|
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)
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 ...
> with(r, lengths * values)
[1] 2 0 2 0 1 0 2 0 3 0 9 0 1 0 7 0
> rr <-with(r, lengths * values)
> rr[rr > 0]
[1] 2 2 1 2 3 9 1 7
See ?rle for more detials.
HTH,
Jorge.-
On Mon, Jul 16, 2012 at 12:27 PM, jcrosbie < [hidden email]> 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 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.
>
> ______________________________________________
> [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.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
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 0 0 0 1 1 0 0 0 1 0
> 1 1 0 1
> 1 1 0 0 0 0
> 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
> 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
>
>
David Winsemius, MD
today: Springdale, UT
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|