Skip to content
Prev 247700 / 398503 Next

dataframe: string operations on columns

Assuming every row is split into exactly two values by whatever string 
you choose as split, one fancy exercise in R data structures is

     dfsplit = function(df, split)
         as.data.frame(
             t(
                 structure(dim=c(2, nrow(df)),
                     unlist(
                         strsplit(split=split,
                             as.matrix(df))))))

so that if your data frame is

     df = data.frame(c('1 2', '3 4', '5 6'))

then

     dfsplit(df, ' ')
     #   V1 V2
     # 1  1  2
     # 2  3  4
     # 3  5  6

renaming the columns left as an exercise.

vQ
On 01/18/2011 05:22 PM, Peter Ehlers wrote: