How to convert information from rle to data frame

I want to convert the information contained in the "rle" function in R to a dataframe, but couldn't find a way. For example, for a vector

x <- c(1,1,1,2,2,3,4,4,4)

I want the dataframe to have two columns 1 2 3 4

and3 2 1 3

Any help would be greatly appreciated!

+3


source to share


3 answers


Use unclass

to remove a class rle

. Then you can just use data.frame

in the resulting list.



data.frame(unclass(rle(x)))
##   lengths values
## 1       3      1
## 2       2      2
## 3       1      3
## 4       3      4

      

+7


source


You can do this with a function data.frame

. rle

actually returns a list of two components ( lengths

and values

).



rleX
data.frame(values = rleX$values, lengths = rleX$lengths)

      

+2


source


Try the following:

data.frame(table(x))

  x Freq
1 1    3
2 2    2
3 3    1
4 4    3

      

+1


source







All Articles