Search code examples
rdata.tablerbindmelt

Faster alternative to melt and rbind


I have a big list of character vectors which looks something like this:

List of 53095
 $ 30875  : chr [1:10] "<h2 class=\"buildings-page-title buildings- ...
 $ 30876  : chr [1:10] "<h2 class=\"buildings-page-title buildings- ...

I want to create a data.table (or a data frame) with a single column. So what I did was:

# require(purr); require(data.table)
clean.data<-function(input){
  output1<-map(input, melt)
  output2<-data.frame()
 for (i in 1:length(output1)) { 
  output2<-rbind(output2, output1[[i]])

 }
 return(output2)}

Using a test data as an example, what I want is

test<-list(c("hello", "world", "!"), c("Nice","to","meet","you"))
print(clean.data(test))
> print(clean.data(test))
  value
1 hello
2 world
3     !
4  Nice
5    to
6  meet
7   you

But this function is incredibly slow, perhaps because my dataset is large, but I think my code is bad. Would there be any other more efficient way to get the same results?


Solution

  • A faster way would be to unlist it and create a single column data.frame

    data.frame(value = unlist(test))