I’m beginning to analyse datas for my thesis. I first need to count consecutive occurences of strings as one. Here’s a sample vector :
test <- c("vv","vv","vv","bb","bb","bb","","cc","cc","vv","vv")
I would like to simply extract unique values, as in the unix command uniq. So expected output would be a vector as :
“vv”,”bb”,”cc”,”vv”
I looked at rle function, wich seems to be fine, but how would I get the output of rle as a vector ? I don’t seem to understand the rle class…
> rle(test)
Run Length Encoding
lengths: int [1:5] 3 3 1 2 2
values : chr [1:5] "vv" "bb" "" "cc" "vv"
How to get one vector of the values output by rle and another one for the lengths ? Hope I’m making myself clear…
Thanks again for any help !
rle()returns a two-element list of class"rle"; as @gsk points out, you can use ordinary list-indexing constructs to access the component vectors.Also, try this, to put the results of
rleinto a more familiar format: