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!
frame() function in R Programming Language is used to convert an object to data frame.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With