Hi:

To spare the OP from tearing his hair out over a nuance of regular
expressions, here's a toy example:

df <- data.frame(key = paste('POSTOFF', 1:3, sep = '_'),
                 as.data.frame(matrix(rpois(180, 10), ncol = 60)))
nms <- scan(what = "")
"key"        "AMR.pa1.M"  "AMR.pa2.M"  "AMR.pa3.M"  "AMR.pa4.M"
"AMR.pa5.M"  "AMR.pa6.M"  "AMR.pa7.M"  "AMR.pa8.M"  "AMR.pa9.M"
"AMR.pa10.M" "AMR.ta1.M"  "AMR.ta2.M"  "AMR.ta3.M"  "AMR.ta4.M"
"AMR.ta5.M"  "AMR.ta6.M"  "AMR.ta7.M"  "AMR.ta8.M"  "AMR.ta9.M"
"AMR.ta10.M" "AMR.ka1.M"  "AMR.ka2.M"  "AMR.ka3.M"  "AMR.ka4.M"
"AMR.ka5.M"  "AMR.ka6.M"  "AMR.ka7.M"  "AMR.ka8.M"  "AMR.ka9.M"
"AMR.ka10.M" "SMR.pa1.M"  "SMR.pa2.M"  "SMR.pa3.M"  "SMR.pa4.M"
"SMR.pa5.M"  "SMR.pa6.M"  "SMR.pa7.M"  "SMR.pa8.M"  "SMR.pa9.M"
"SMR.pa10.M" "SMR.ta1.M"  "SMR.ta2.M"  "SMR.ta3.M"  "SMR.ta4.M"
"SMR.ta5.M"  "SMR.ta6.M"  "SMR.ta7.M"  "SMR.ta8.M"  "SMR.ta9.M"
"SMR.ta10.M" "SMR.ka1.M"  "SMR.ka2.M"  "SMR.ka3.M"  "SMR.ka4.M"
"SMR.ka5.M"  "SMR.ka6.M"  "SMR.ka7.M"  "SMR.ka8.M"  "SMR.ka9.M"
"SMR.ka10.M"

names(df) <- nms
dfm <- melt(df2, id = 'key')
dfm2 <- cbind(dfm, colsplit(dfm$variable, split = '\\.', names = c('A', 'B',
'C')))
head(dfm2)

That little \\ in front of the . in the split argument of colsplit matters.
It took me about 10 minutes for the obvious to dawn on me...that the value
fed to split has to obey regular expression rules :-}

HTH,
Dennis

On Thu, Jan 20, 2011 at 1:01 PM, Hadley Wickham <had...@rice.edu> wrote:

> >> I think I should be able to do this using the "reshape" function, but
> >> I cannot get it to work. I think I need some help to understand
> >> this...
> >>
> >>
> >> (If I could split the "variable" into three separate columns splitting
> >> by ".", that would be even better.)
> >
> > Use strsplit and "["
>
> Or colsplit, from reshape, that does this for you.
>
> Hadley
>
> --
> Assistant Professor / Dobelman Family Junior Chair
> Department of Statistics / Rice University
> http://had.co.nz/
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to