Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert information from rle into a data frame

Tags:

r

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

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

I want a dataframe that has two columns of 1 2 3 4 and 3 2 1 3

Any help would be greatly appreciated!

like image 294
Euan Ritchie Avatar asked Jun 30 '15 13:06

Euan Ritchie


People also ask

How do I convert data into a Dataframe in R?

frame() function in R Programming Language is used to convert an object to data frame.

How do I convert a matrix to a Dataframe in R?

A matrix can be converted to a dataframe by using a function called as. data. frame(). It will take each column from the matrix and convert it to each column in the dataframe.

What is RLE in R programming?

Description. The Rle class is a general container for storing an atomic vector that is stored in a run-length encoding format. It is based on the rle function from the base package.

How do I create a data frame in R?

We can create a dataframe in R by passing the variable a,b,c,d into the data. frame() function. We can R create dataframe and name the columns with name() and simply specify the name of the variables.


1 Answers

Use unclass to remove the rle class. Then you can just use data.frame on the resulting list.

data.frame(unclass(rle(x)))
##   lengths values
## 1       3      1
## 2       2      2
## 3       1      3
## 4       3      4
like image 178
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 23 '22 11:10

A5C1D2H2I1M1N2O1R2T1