Skip to content
Prev 2737 / 21312 Next

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

Hi Andreas,
I too, have a solution. Mine relies on the idiom of "do.call(rbind, FUN)" (or cbind), which efficiently rbinds/cbinds the results from FUN. For this to work, FUN must return a list.
First we define the function that does the actual splitting, returning a (sub-)matrix for each row, `splitit`. The we use `apply` to apply `splitit` on each row in the matrix; this returns the desired list that we can put into the do.call idiom.

splitit <- function(row) { 
    # Split the symbols (res is a normal vector)
    res <- strsplit(row[2], ' // ', fixed=TRUE)[[1]]
    # Bind the ID to the symbol vector, clean up the result and return
    res <- t(rbind(row[1], res))	# If the original data contains several columns, just add them here.
    dimnames(res) <- list(c(), c())
    return(res)
}

# Test data from Heidi Dvinge and James W. MacDonald
test <- data.frame(A=c("x", "y", "z"), B=c("X", "Y1 // Y2 // Y3", "Z"),  stringsAsFactors=FALSE)
df <- data.frame(ID = 1:4, Symbol = I(c("Bla","Foo","XYZ // xyz // xyz01", "abc")))

do.call(rbind, apply(test, 1, splitit))
do.call(rbind, apply(df, 1, splitit))


Kind regards,
Stefan McKinnon H?j-Edwards     Dept. of Genetics and Biotechnology
PhD student                     Faculty of Agricultural Sciences
stefan.hoj-edwards at agrsci.dk    Aarhus University
Tel.: +45 8999 1291             Blichers All? 20, Postboks 50
Web: www.iysik.com              DK-8830?Tjele
                                Tel.: +45 8999 1900
                                Web: www.agrsci.au.dk