[R] split character string in matrix into character vector and numeric vector

Christos Hatzis christos.hatzis at nuverabio.com
Wed May 21 19:26:53 CEST 2008


> x <- "B03_MAH     0.2115  0.2087  0.2087  0.2147  0.2115  0.2176"
> strsplit(x, " +")
[[1]]
[1] "B03_MAH" "0.2115"  "0.2087"  "0.2087"  "0.2147"  "0.2115"  "0.2176" 

-Christos

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Kim Milferstedt
> Sent: Wednesday, May 21, 2008 1:11 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] split character string in matrix into character 
> vector and numeric vector
> 
> Hello,
> 
> I've got a matrix consisting of one column with n rows. Each 
> field in the matrix is filled with a character vector.
> 
> I would like to convert this matrix into a character vector 
> containing the B03_MAH-type entries from the beginning of 
> each row and a data.frame that contains the numeric data, but 
> I am stuck.
> 
> I have tried to use textConnection with sep = "  " but since 
> there is sometimes more than two spaces separating the 
> entries, I obtain a result with columns full of NAs.
> 
> Then I tried sub() to replace the all multi-spaces to get 
> only one space that separates each entry but that didn't work either.
> 
> Do you know a good way to convert this matrix?
> 
> Thanks already,
> 
> Kim
> 
> taxa.vec "B03_MAH     0.2115  0.2087  0.2087  0.2147  0.2115  0.2176" 
> taxa.vec "F02_SSBH    0.2162  0.2191  0.2133  0.2130  0.2220  0.2158" 
> taxa.vec "G04_MAH     0.2083  0.2111  0.2055  0.2115  0.2140  0.2087" 
> taxa.vec "A04_MAH     0.2097  0.2126  0.2069  0.2065  0.2097  0.2037" 
> taxa.vec "E03_MAH     0.2097  0.2069  0.2069  0.2130  0.2155  0.2101"
> 
> ______________________________________________
> 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.
> 
>



More information about the R-help mailing list