separate multiple columns each into multiple columns

245 views
Skip to first unread message

Bob Tao

unread,
Sep 29, 2016, 3:45:01 PM9/29/16
to manipulatr
I have a data frame with a few columns and I would like to separate each column into 3 separate columns.

library(tidyr)
x <- data.frame(x1=c('1:2:3','2:3:4'),x2=c('5:6:7','7:8:9'))

tidyr::separate(data=x,col='x1',into=c('a','b','c'),sep=':')
  a b c    x2
1 1 2 3 5:6:7
2 2 3 4 7:8:9

tidyr::separate(data=x['x1'],col='x1',into=c('a','b','c'),sep=':')
  a b c
1 1 2 3
2 2 3 4


I would like to apply it to all columns, the following did not work

lapply(names(x),function(i) {tidyr::separate(data=x[i],col=i,into=c('a','b','c'),sep=':')})
Error in if (!after) c(values, x) else if (after >= lengx) c(x, values) else c(x[1L:after], : argument is of length zero


Any pointers are appreciated!

p.s. This is cross posted to R Shiny group before I became aware of this group.

Ista Zahn

unread,
Sep 29, 2016, 4:18:44 PM9/29/16
to Bob Tao, manipulatr

Hi Bob,

Use separate_

Best,
Ista


--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+unsubscribe@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at https://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.

Bob Tao

unread,
Sep 30, 2016, 10:07:46 AM9/30/16
to manipulatr, bob...@gmail.com
Thanks so much, Ista!
I'll need to go back to read the doc again!
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages