Skip to content

[Bioc-devel] question on annotations and data.frames

4 messages · Heidi Dvinge, James W. MacDonald, Andreas Heider

#
Hi Andreas,
This isn't particularly elegant, but might get you what you want:

test <- data.frame(A=c("x", "y", "z"), B=c("X", "Y1 // Y2 // Y3", "Z"),
stringsAsFactors=FALSE)

test.split <- strsplit(test$B, " // ", fixed=TRUE)

test2 <- test[rep(1:nrow(test), times=sapply(test.split, length)),]

test2$B	<- unlist(test.split)

HTH
\Heidi
#
Hi Andreas,
On 8/23/2011 8:12 AM, Andreas Heider wrote:
Here is one way:

 > df <- data.frame(ID = 1:4,
Symbol = I(c("Bla","Foo","XYZ // xyz // xyz01", "abc")))

 > lst <- tapply(1:nrow(df), df$ID, function(x) df[x,2])
 > lst <- lapply(lst, function(x) strsplit(x, " // "))
 > newdf <- data.frame(ID = rep(df[,1], sapply(lst, length)), Symbol = 
unlist(lst))
 > newdf
    ID Symbol
1   1    Bla
2   2    Foo
31  3    XYZ
32  3    xyz
33  3  xyz01
4   4    abc

Best,

Jim