Hi.
I completely forgot split function so loop:

l <- vector("list", n_levels)
for(i in 1:n_levels)
{
l[[i]] <- a[blocks==levels(blocks)[i]]
}
l

could be substitute with:

split(a, blocks),

but anyway Rui's solution is better.

Andrija


On Thu, Apr 18, 2013 at 1:18 PM, andrija djurovic <djandr...@gmail.com>wrote:

> Hi Igor.
>
> Here is one way:
>
> DF <- read.table(textConnection("90.1194354
> 87.94788274
> 80.34744843
> 64.06080347
> 30.40173724
> 0
> 0
> 0
> 0
> 0
> 16.28664495
> 23.88707926
> 29.31596091
> 48.85993485
> 13.02931596
> 0
> 0
> 0
> 7.600434311
> 20.62975027
> 29.31596091
> 32.5732899"), header=FALSE)
>
> a <- DF$V1[which(DF$V1!=0)]
> indx <- which(DF$V1!=0)
>
> blocks <- cut(1:length(a), breaks=c(0,which(diff(indx)!=1), length(a)))
> n_levels <- length(levels(blocks))
>
> l <- vector("list", n_levels)
> for(i in 1:n_levels)
> {
> l[[i]] <- a[blocks==levels(blocks)[i]]
> }
> l
>
> Andrija
>
>
> On Thu, Apr 18, 2013 at 11:33 AM, Igor Mintz <igormi...@gmail.com> wrote:
>
>> hello
>> i have a very long column of numbers. i want R to make a new column every
>> time the value changes from zero.
>> example for the column:
>> 90.1194354
>> 87.94788274
>> 80.34744843
>> 64.06080347
>> 30.40173724
>> 0
>> 0
>> 0
>> 0
>> 0
>> 16.28664495
>> 23.88707926
>> 29.31596091
>> 48.85993485
>> 13.02931596
>> 0
>> 0
>> 0
>> 7.600434311
>> 20.62975027
>> 29.31596091
>> 32.5732899
>>
>> for this example i want to get 3 columns.
>>
>> thanks!
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@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.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to