Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use rbindlist(data) instead of do.call(rbind, data) in this case

library(dplyr)
library(data.table)
library(stringr)

test = c('a1b1', 'a2b2', 'a3b3')
result = rbind(c(1,1),
               c(2,2),
               c(3,3))
result
     [,1] [,2]
[1,]    1    1
[2,]    2    2
[3,]    3    3
test2<-do.call(rbind,test %>% str_split('a'))
test3<-do.call(rbind,test2 %>% .[,2] %>% str_split('b'))
test3
     [,1] [,2]
[1,] "1"  "1" 
[2,] "2"  "2" 
[3,] "3"  "3" 
  1. do.call(rbind, data) is not equal rbindlist(data) ?
    data.table::rbindlist is not working. If I want to use rbindlist, what can I do?
rbindlist(test %>% str_split('a'))
Error in rbindlist(test %>% str_split("a")) : 
  Item 1 of input is not a data.frame, data.table or list

>Solution :

If you want to use a similar approach using rbindlist, then you could do something like below. Essentially, you can add in a step to to turn each item in the list into a data.table (but need to transpose first).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(dplyr)
library(data.table)
library(stringr)

test2 <- rbindlist(test %>% str_split("a") %>% lapply(., function(x)
  as.data.table(t(x))))

test3 <- rbindlist(as.matrix(test2) %>% .[,2] %>% str_split("b") %>% lapply(., function(x)
  as.data.table(t(x))))

Output

test3

   V1 V2
1:  1  1
2:  2  2
3:  3  3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading