r - Separate Comma Delimited Cells To New Rows -


hi have table comma delimited columns , need convert comma delimited values new rows. exmaple given table

name     start      end         1,2,3    4,5,6 b          1,2      4,5 c      1,2,3,4  6,7,8,9    

i need convert like

name start end        1   4        2   5          3   6    b     1   4    b     2   5     c     1   6    c     2   7    c     3   8       c     4   9 

i can using vb script need solve using r can solve this?

here's approach should work you. i'm assuming 3 input vectors in different objects. going create list of inputs , write function process each object , returns them in form of data.frame plyr.

the things take note of here splitting of character vector it's component parts, using as.numeric convert numbers character form when split. since r fills matrices column, define 2 column matrix , let r fill values us. retrieve name column , put in data.frame. plyr nice enough process list , convert data.frame automatically.

library(plyr)  <- paste("a",1, 2,3,4,5,6, sep = ",", collapse = "") b <- paste("b",1, 2,4,5, sep = ",", collapse = "") c <- paste("c",1, 2,3,4,6,7,8,9, sep = ",", collapse = "")  input <- list(a,b,c)  splitter <- function(x) {     x <- unlist(strsplit(x, ","))     out <- data.frame(x[1], matrix(as.numeric(x[-1]), ncol = 2))     colnames(out) <- c("name", "start", "end")     return(out) }   ldply(input, splitter) 

and output:

> ldply(input, splitter)  name start end 1        1   4 2        2   5 3        3   6 4    b     1   4 5    b     2   5 6    c     1   6 7    c     2   7 8    c     3   8 9    c     4   9 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -