Daniel Brewer
2010-Nov-19 12:04 UTC
[R] expand comma separated field vertically in data.frame
Hello, I have a data.frame like this: a 1,2,3,4 b1 b 6,7 b3 And I would like to transform it to this: a 1 b1 a 2 b1 a 3 b1 a 4 b1 b 6 b3 b 7 b3 I have been looking at ddply but can't seem to work it out. ANy help would be gratefully received. Dan -- ************************************************************** Daniel Brewer, Ph.D. Institute of Cancer Research Molecular Carcinogenesis Email: daniel.brewer at icr.ac.uk ************************************************************** The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP. This e-mail message is confidential and for use by the a...{{dropped:2}}
Henrique Dallazuanna
2010-Nov-19 12:12 UTC
[R] expand comma separated field vertically in data.frame
Try this: transform(x[rep(seq(nrow(x)), sapply(v.s <- strsplit(as.character(x$V2), ","), length)),], V2 = unlist(v.s)) On Fri, Nov 19, 2010 at 10:04 AM, Daniel Brewer <daniel.brewer@icr.ac.uk>wrote:> Hello, > > I have a data.frame like this: > a 1,2,3,4 b1 > b 6,7 b3 > > And I would like to transform it to this: > a 1 b1 > a 2 b1 > a 3 b1 > a 4 b1 > b 6 b3 > b 7 b3 > > I have been looking at ddply but can't seem to work it out. ANy help > would be gratefully received. > > Dan > > -- > ************************************************************** > Daniel Brewer, Ph.D. > > Institute of Cancer Research > Molecular Carcinogenesis > Email: daniel.brewer@icr.ac.uk > ************************************************************** > > The Institute of Cancer Research: Royal Cancer Hospital, a charitable > Company Limited by Guarantee, Registered in England under Company No. 534147 > with its Registered Office at 123 Old Brompton Road, London SW7 3RP. > > This e-mail message is confidential and for use by the...{{dropped:20}}