Re: [R] Best way to strsplit a column

From: Satoshi Takahama <>
Date: Tue, 04 Mar 2008 06:15:30 -0800 (PST)

Hi Daniel,

After using strsplit() you can call a user-written function to extend the length of each list element to a uniform value and then use with rbind. For instance,

> txt <- "1:2

+ 3:4
+ 5
+ 6:7"

> x<- readLines(textConnection(txt))
> f <- function(x)
+           lapply(x,function(a,n) c(a,rep(NA,n-length(a))),
+                  n = max(sapply(x,length))))

> f(strsplit(x,":"))
[,1] [,2] [1,] "1" "2" [2,] "3" "4"

[3,] "5" NA
[4,] "6" "7"

Hope this helps,



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 a...{{dropped:16}} mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 04 Mar 2008 - 14:24:53 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 - 14:30:20 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive