Skip to content
Prev 320488 / 398506 Next

Read text file in R

HI,
Just a correction:
indx<-rep(rep(c(TRUE,FALSE),each=2),23)
A.K.




----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Joanna Zhang <zjoanna2013 at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Thursday, March 28, 2013 11:38 AM
Subject: Re: [R] Read text file in R


con<-file("RRoutall.txt")
?Lines1<- readLines(con)
?close(con)
indx<-rep(rep(c(TRUE,FALSE),each=2),22)
Lines2<-Lines1[!grepl("[A-Za-z]",Lines1)]
res<-read.table(text=paste(gsub("^\\s+","",Lines2[indx]),gsub("^\\s+","",Lines2[!indx])),sep="",header=FALSE)
nm1<-unlist(strsplit(gsub("^ +","",paste(Lines1[grepl("[A-Za-z]",Lines1)][1:2],collapse=" "))," "))
colnames(res)<- nm1[nm1!=""]
head(res)
#? m1 n1? m? n cterm1_P0L cterm1_P0H? c11? c12?? c1?? c2 alpha beta T_error? N
#1? 8? 4 12 10??? 0.68152??? 0.53348 0.03 0.03 0.15 0.15?? 0.1? 0.2 0.41218 22
#2? 8? 4 12 10??? 0.68152??? 0.53348 0.03 0.03 0.15 0.15?? 0.1? 0.2 0.41218 22
#3? 8? 4 12? 9??? 0.68152??? 0.53348 0.03 0.03 0.15 0.20?? 0.1? 0.2 0.42356 21
#4? 8? 4 12? 9??? 0.68152??? 0.53348 0.03 0.03 0.15 0.20?? 0.1? 0.2 0.42356 21
#5? 8? 4 12? 8??? 0.68152??? 0.53348 0.03 0.03 0.15 0.25?? 0.1? 0.2 0.47058 20
#6? 8? 4 12? 8??? 0.68152??? 0.53348 0.03 0.03 0.15 0.25?? 0.1? 0.2 0.47058 20
?# ??? EN?????? BH????? BL?????? AH?????? AL
#1 16.073 0.086514 0.19448 0.092756 0.038431
#2 16.073 0.086514 0.19448 0.092756 0.038431
#3 15.607 0.131028 0.18599 0.064031 0.042514
#4 15.607 0.131028 0.18599 0.064031 0.042514
#5 15.140 0.193348 0.19418 0.043383 0.039667
#6 15.140 0.193348 0.19418 0.043383 0.039667

A.K.