[R] 'Split' character

Sébastien pomchip at free.fr
Mon Nov 26 22:53:01 CET 2007


Sure, it helps ! I did not get that my 'do.call' was coerced to a factor 
and that the levels of the factor were used to order the list...

Thank you very much.

Dimitris Rizopoulos a écrit :
> one way is the following:
>
> vals <- do.call(paste, c(mydata[1:2], sep = ":"))
> fc <- factor(vals, levels = unique(vals))
> mydata.split <- split(mydata, fc, drop = TRUE)
> names(mydata.split)
>
>
> I hope it helps.
>
> Best,
> Dimitris
>
> ----
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://med.kuleuven.be/biostat/
>      http://www.student.kuleuven.be/~m0390867/dimitris.htm
>
>
> Quoting Sébastien <pomchip at free.fr>:
>
>> One additionnal question about a problem that I just noticed in my 
>> script:
>> the list created by my split function is sorted in the increasing order
>> and not in the current order of the elements of the do.call argument
>> (see the results of the two last lines of the following script). I was
>> unsuccesful in tweaking my split function to have a proper order...
>> If there is no way to do that, I guess it comes down to a very basic
>> question: how can I reorder a list ?
>>
>> ####
>> how can I reorder the list 'mydata.split' according to
>>
>> a<-rep(1.5:10.5,each=2)
>> b<-rep(21:30,each=2)
>> cd<-101:120
>> mydata<-data.frame(a,b,cd)
>>
>> mydata.split <- split(mydata,do.call(paste,c(mydata[,1:2],sep =
>> ":")),drop=TRUE)
>> names(mydata.split)
>> unique(do.call(paste,c(mydata[,1:2],sep = ":")))
>>
>> ####
>>
>> Sebastien
>>
>> Gabor Grothendieck a écrit :
>>> Try this using the BOD data frame that comes with R or
>>> try using interaction in place of paste:
>>>
>>> split(BOD, do.call(paste, c(BOD, sep = ":")), drop=TRUE)
>>>
>>>
>>> On Nov 25, 2007 10:23 AM,  <pomchip at free.fr> wrote:
>>>
>>>> Thanks Jim and Herinque for you reply.
>>>>
>>>> The substitution you are both proposing works fine, except when a  
>>>> or b contains
>>>> decimal numbers (which is mostly my case in a real dataset). In  
>>>> this case, there
>>>> is no distinction between the decimal point and the one introduced 
>>>>  by the split
>>>> function.
>>>> If the split function cannot be customized, then I guess I have to 
>>>> first
>>>> substitute the decimal point in my dataset by e.g. a * sign and  
>>>> change it back
>>>> to a . when the split is done...
>>>>
>>>> Sebastien
>>>>
>>>> jim holtman a écrit :
>>>>
>>>>
>>>>> Here is one way:
>>>>>
>>>>>
>>>>>> a<-1:10
>>>>>> b<-21:30
>>>>>> mydata<-data.frame(a,b)
>>>>>> mydata.split<-split(mydata,(mydata[,1:2]),drop=TRUE)
>>>>>> x <- mydata.split
>>>>>> # customize the names
>>>>>> names(x) <- sapply(strsplit(names(x), "\\."), paste, collapse="-my
>>>>>>
>>>> character-")
>>>>
>>>>>> x
>>>>>>
>>>>> $`1-my character-21`
>>>>>   a  b
>>>>> 1 1 21
>>>>>
>>>>> $`2-my character-22`
>>>>>   a  b
>>>>> 2 2 22
>>>>>
>>>>> $`3-my character-23`
>>>>>   a  b
>>>>> 3 3 23
>>>>> ...........
>>>>>
>>>>> On Nov 24, 2007 6:15 PM,  <pomchip at free.fr> wrote:
>>>>>
>>>>>> Dear R-users,
>>>>>>
>>>>>> The following code splits a very simple dataframe into a list,  
>>>>>> each element
>>>>>>
>>>> of
>>>>
>>>>>> the list being one line of the dataframe. You will see that the 
>>>>>> split
>>>>>>
>>>> function
>>>>
>>>>>> names each element of the list by using uses the content of a and 
>>>>>> b and
>>>>>>
>>>> merging
>>>>
>>>>>> them with a "." character. Is there a way to customize this 
>>>>>> character?
>>>>>>
>>>>>> a<-1:10
>>>>>> b<-21:30
>>>>>> mydata<-data.frame(a,b)
>>>>>> mydata.split<-split(mydata,(mydata[,1:2]),drop=TRUE)
>>>>>> mydata.split
>>>>>>
>>>>>> Thanks in advance for your help.
>>>>>>
>>>>>> Sebastien
>>>>>>
>>>>>> ______________________________________________
>>>>>> 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.
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>>
>>>
>>>
>>>
>>
>>     [[alternative HTML version deleted]]
>>
>>
>
>
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>
>
>



More information about the R-help mailing list