Re: [R] Best way to strsplit a column

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Tue, 04 Mar 2008 15:06:11 +0100

try the following:

strg <- c("123:abc", "qwe:789f", "abcde", "a:fd", "567") sapply(strsplit(strg, ":"), function(x){

    if (length(x) == 1) x <- c(x, NA)
    x
})

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
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


> Hello,
>
> I have a data.frame with a column that I would like to split into
> based
> around the delimiter ":". This is a useful feature in Excel. I
> cannot
> work out the best way to do it in R. I am sure you need to use
> strsplit, but that returns a list. The problem is that some values
> in
> the column do not contain a ":" so should have a "NA" in the second
> column of the result, and this makes doing an unlist a non-starter.
>
> Any ideas?
>
> Many thanks
>
> Daniel Brewer
>
> The Institute of Cancer Research: Royal Cancer Hospital, a
> charitable Company Limited by Guarantee, Registered in England under
> Company No. 534147 with its Registered Office at 123 Old Brompton
> Road, London SW7 3RP.
>
> This e-mail message is confidential and for use by the...{{dropped:14}}



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 Tue 04 Mar 2008 - 14:20:23 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 Tue 04 Mar 2008 - 15:30:19 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