[R] how to separate stuck row elements?

arun smartpink111 at yahoo.com
Fri Nov 30 17:07:02 CET 2012


Hi,
Try this:
Lines<-"-100 -100-3456-3456-3456-100 -100
23 -3456-3456-189 34 56 78
-100 34 56 21 44 65 78
"
res<-unlist(strsplit(gsub("\\-"," -",Lines),"\n"))
res1<-do.call(rbind,lapply(lapply(split(res,seq_along(res)),function(x) unlist(strsplit(x," "))),function(x) as.numeric(x[x!=""])))
 res1
##  [,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

 res2<-data.frame(res1)
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