[R] transforming one column into 2 columns

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat Feb 2 19:03:14 CET 2008


Benilton Carvalho wrote:
> help("strsplit")
> b
>
Yes, but...

The postprocessing gets a bit awkward. It might be easier to use sub() 
to get rid of the first/last bit of the string i.e.

C2 <- sub("^.*:", "",  Col)
C1 <- sub(":.*$", "",  Col)

An orthogonal idea is

con <- textConnection("Col")
read.table(con, sep=":")
close(con)

> On Feb 2, 2008, at 12:43 PM, joseph wrote:
>
>>
>>
>> Hello
>>
>> I have a data frame and one of its columns is as follows:
>>
>>
>>
>>
>> Col
>>
>>
>> chr1:71310034
>>
>>
>>  
>>
>> chr15:37759058
>>
>>
>> chr22:18262638
>>
>>
>> chrUn:31337214
>>
>>
>> chr10_random:4369261
>>
>>
>> chrUn:3545097
>>
>>
>>
>>
>>
>> I would like to get rid of colon (:) and replace this column
>> with two new columns containing the terms on each side of the colon. 
>> The new columns
>> should look as follows:
>>
>>
>>
>>
>> Col_a   Col_b
>>
>>
>> chr1     71310034
>>
>>
>> chr14   23354088
>>
>>
>> chr15   37759058
>>
>>
>> chr22   18262638
>>
>>
>> chrUn   31337214
>>
>>
>> chr10_random  4369261
>>
>>
>> chrUn   3545097
>>
>>
>>
>>
>>
>> Any help will be much appreciated
>>
>>
>> Joseph
>>
>>
>>
>>
>>
>>
>>      
>> ____________________________________________________________________________________ 
>>
>> Looking for last minute shopping deals?
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>   


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list