[R] subsets of vector
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Mar 24 14:51:42 CET 2006
Use rle:
with(rle(x), data.frame(value = values,
start = cumsum(lengths)-lengths+1, end = cumsum(lengths))[lengths > 1,])
On 3/23/06, Fred J. <phddas at yahoo.com> wrote:
> dear R users
> I have been trying a lot to get this task accomplished but need your help.
>
> input:
> x <- c(3,3,4,4,4,4,5,7,7,0,1,1,2,2,2)
>
> desired output
> 3 1 2
> 4 3 6
> 7 8 9
> ...
> which means,
> 3 is repeated at index 1:2
> 4 is repeated at index 3:6
> ...
> my effort has not been that good.
> a <- !c(NA,diff(x));
> b <- which(a==1);
> a[b-1]=1;
> cbind(x,a);
>
> or
> sp <- (seq(diff(x))[!!cumsum(!!diff(x))])[1];sp
> #first locations of duplicates
> l <- which(diff(x[sp:length(x)])==0); l
> m <- which(diff(l)==1);m
> flod <- l[m]+1; print(flod);
>
>
> thank you
>
> ---------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list