Re: [R] help with colsplit (reshape)

From: Ista Zahn <istazahn_at_gmail.com>
Date: Fri, 13 Jun 2008 15:45:56 -0400

Thanks Hadley, with your help I'm getting things figured out. On Jun 13, 2008, at 2:09 PM, hadley wickham wrote:

>> M.Data2 <- data.frame(M.Data, colsplit(M.Data$variable, split = "\
>> \.", names
>> = c("treatment", "time")))
>>
>> which gave:
>>
>> head(M.Data2)
>> pid variable value treatment time
>> 1 1 predA -1 predA predA
>> 2 2 predA -2 predA predA
>> 3 3 predA -1 predA predA
>> 4 4 predA -2 predA predA
>> 5 5 predA -1 predA predA
>> 6 6 predA -2 predA predA
>>
>> Closer but no cigar.
>
> Have a look at the whole thing - it's getting it right most of the
> time. Going back to the original variable names, I see that "PredA"
> does not have a time associated with it. What do you expect the time
> to be?
Right, there is no time associated with this variable. So I tried again, treating it as an id:

M.Data <- melt(Data, id = c("pid", "predA"))

 From here I was able to achieve the desired result, as follows:

M.Data <- data.frame(M.Data, colsplit(M.Data$variable, split = "\\.", names=c("measure", "time")))

M.Data$variable <- M.Data$measure
M.Data <- M.Data[-5]
L.Data <- cast(M.Data, ... ~ variable)

This is perhaps a bit inelegant but it works! I'm interested in knowing if there is a better way to do it, but I'm happy that I've at least figured out this much. As always I'm humbled by the generosity of people who not only make their software available but also take the time to answer questions on this list. Thank you!

-Ista
>
>
>> I would be grateful if someone will tell me (a) how to reshape the
>> data as
>> described above using the reshape package, (b) what difference
>> between split
>> = "." and split = "\\." is,
>
> The splitting argument is a regular expression, and in regular
> expression speak "." means to match any one character. "\\." escapes
> the full stop, so it only matches full stops.
>
>> and (c) if more information about the colsplit
>> command is available anywhere.
>
> Probably the best way is just to look at the code (it's pretty
> simple):
>
>> colsplit.character
> function (x, split = "", names)
> {
> vars <- as.data.frame(do.call(rbind, strsplit(x, split)))
> names(vars) <- names
> as.data.frame(lapply(vars, function(x)
> type.convert(as.character(x))))
> }
>
> If strsplit doesn't do what you want, you might need to write your own
> function following those lines.
>
> Hadley
>
> --
> http://had.co.nz/



R-help_at_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. Received on Fri 13 Jun 2008 - 22:04:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 13 Jun 2008 - 23:31:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive