[R] dividing a long column to many short ones by a condition

Rui Barradas ruipbarradas at sapo.pt
Thu Apr 18 13:31:25 CEST 2013


Hello,

Sorry, forgot the instruction to read in the data.
Now the complete code.


x <- scan(text = "
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
")
x

grp <- cumsum(abs(c(0, diff(x == 0))))
tmp <- lapply(split(x, grp), function(x) if(all(x == 0)) NULL else x)
tmp[sapply(tmp, function(x) !is.null(x))]



Rui Barradas

Em 18-04-2013 12:24, Rui Barradas escreveu:
> Hello,
>
> Something like this?
>
>
> grp <- cumsum(abs(c(0, diff(x == 0))))
> tmp <- lapply(split(x, grp), function(x) if(all(x == 0)) NULL else x)
> tmp[sapply(tmp, function(x) !is.null(x))]
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 18-04-2013 10:33, Igor Mintz escreveu:
>> 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 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.
>>
>
> ______________________________________________
> 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