r - Create name-value pair data frame from input with delimiters -


i have data frame can created code:

input <- data.frame( 'id'=c(1:3),                        destination=c("a\r\nb", "c", "d\r\ne\r\nf"),                        topic=c("w", "x", "y\r\nz") ) 

it looks this:

  id destination  topic 1  1      a\r\nb      w 2  2           c      x 3  3 d\r\ne\r\nf y\r\nz 

i create output data frame looks this:

desiredoutput <- data.frame(     id = c(1,1,1,2,2,3,3,3,3,3) ,     name=c( "destination", "destination", "topic", "destination", "topic",            "destination", "destination", "destination" , "topic", "topic"),     value=c("a","b", "w", "c", "x", "d", "e", "f", "y", "z") )     id        name value 1   1 destination     2   1 destination     b 3   1       topic     w 4   2 destination     c 5   2       topic     x 6   3 destination     d 7   3 destination     e 8   3 destination     f 9   3       topic     y 10  3       topic     z 

whenever delimiter \r\n occurs, split contents separate rows, correct id, name of column, , corresponding value.

i can split single column list using strsplit, don't know how put contents data frame above apart attempting write loop. expect tidyr package might helpful.

strsplit(input$destination, split = "\r\n") 

how can done, ideally without loop?

with tidyr, gather long form, use separate_rows separate joined elements:

library(tidyr)  input %>% gather(name, value, -id) %>% separate_rows(value) ##    id        name value ## 1   1 destination     ## 2   1 destination     b ## 3   2 destination     c ## 4   3 destination     d ## 5   3 destination     e ## 6   3 destination     f ## 7   1       topic     w ## 8   2       topic     x ## 9   3       topic     y ## 10  3       topic     z 

note: if data factors instead of character, tidyr warn you, coerces character in order rearrange. work regardless, if hate warnings, coerce character manually before reshaping.


Comments