[R] how to separate stuck row elements?

arun smartpink111 at yahoo.com
Fri Nov 30 17:56:21 CET 2012


Hi,
You could also try this:
Lines<-"-100 -100-3456-3456-3456-100 -100
23 -3456-3456-189 34 56 78
-100 34 56 21 44 65 78"
Lines1<-readLines(textConnection(Lines))
 res1<-unlist(strsplit(gsub("[-]"," -",Lines2)," "))
 matrix(as.numeric(res1[res1!=""]),nrow=length(Lines1),byrow=TRUE)
    [,1]  [,2]  [,3]  [,4]  [,5] [,6] [,7]
#[1,] -100  -100 -3456 -3456 -3456 -100 -100
#[2,]   23 -3456 -3456  -189    34   56   78
#[3,] -100    34    56    21    44   65   78


A.K.

----- Original Message -----
From: Jaime Otero Villar <j.o.villar at bio.uio.no>
To: r-help at r-project.org
Cc: 
Sent: Friday, November 30, 2012 7:55 AM
Subject: [R] how to separate stuck row elements?

Hi,

I was wondering if it's possible to separate elements in multiple rows that actually should appear in different columns. I have a file where in certain lines there are elements not separated, and they certainly should appear in different columns (an example of the file is attached). The point is that I do not want to manually add a space in the txt file, however, I did not manage to do it automatically in R...

Thanks in advance for any insight.

Jaime





______________________________________________
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