[R] Read text file in R

arun smartpink111 at yahoo.com
Thu Mar 28 16:41:53 CET 2013


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.



________________________________
From: Joanna Zhang <zjoanna2013 at gmail.com>
T

o: arun <smartpink111 at yahoo.com> 
Sent: Thursday, March 28, 2013 11:26 AM
Subject: Re: Read text file in R


Hi,

got another output file, there is an error when reading it using the code:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 47 did not have 19 elements




On Mon, Mar 25, 2013 at 12:32 PM, arun <smartpink111 at yahoo.com> wrote:


>
>res[res$EN==min(res$EN),]
>
>#   m1 n1  m  n cterm1_P0L cterm1_P0H  c11  c12   c1   c2 alpha beta   T_error
>#25  9  4 13 12  0.6302494  0.7565041 0.03 0.07 0.15 0.15   0.1  0.2 0.4403712
>#27  9  4 13 12  0.6302494  0.7565041 0.03 0.07 0.15 0.20   0.1  0.2 0.4473437
>#33  9  4 13 12  0.6302494  0.7565041 0.05 0.07 0.15 0.15   0.1  0.2 0.4403712
>#35  9  4 13 12  0.6302494  0.7565041 0.05 0.07 0.15 0.20   0.1  0.2 0.4473437
>
>    N       EN        BH        BL         AH         AL
>#25 25 16.42697 0.1201138 0.1933632 0.09321455 0.03367957
>#27 25 16.42697 0.1553998 0.1933632 0.06490110 0.03367957
>#33 25 16.42697 0.1201138 0.1933632 0.09321455 0.03367957
>#35 25 16.42697 0.1553998 0.1933632 0.06490110 0.03367957
>
>A.K.
>
>
>
>________________________________
>From: Joanna Zhang <zjoanna2013 at gmail.com>
>To: arun <smartpink111 at yahoo.com>
>Sent: Monday, March 25, 2013 12:13 PM
>Subject: Re: Read text file in R
>
>
>
>Great! When I tried to extract the min of EN, it has an error;
>
>
>
>opt<-con[con$EN==min(con$EN),]
>opt
>Error in con$EN : $ operator is invalid for atomic vectors
>
>
>
>
>On Mon, Mar 25, 2013 at 11:05 AM, arun <smartpink111 at yahoo.com> wrote:
>
>Hi,
>>Try this:
>>con<-file("Routallnew.txt")
>> Lines1<- readLines(con)
>> close(con)
>>indx<-rep(rep(c(TRUE,FALSE),each=2),24)
>>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 13  5 17  9  0.7851203  0.6689925 0.03 0.03 0.15 0.15   0.1  0.2 0.3669373 26
>>#2 13  5 17  9  0.7851203  0.6689925 0.03 0.03 0.15 0.15   0.1  0.2 0.3669373 26
>>#3  9  5 13 11  0.6302494  0.4876750 0.03 0.03 0.15 0.20   0.1  0.2 0.4137296 24
>>#4  9  5 13 11  0.6302494  0.4876750 0.03 0.03 0.15 0.20   0.1  0.2 0.4137296 24
>>#5  9  5 13 11  0.6302494  0.4876750 0.03 0.03 0.15 0.25   0.1  0.2 0.4782406 24
>>#6  9  5 13 11  0.6302494  0.4876750 0.03 0.03 0.15 0.25   0.1  0.2 0.4782406 24
>>#        EN         BH        BL         AH         AL
>>#1 20.18355 0.07718537 0.1865207 0.08079875 0.02243240
>>#2 20.18355 0.07718537 0.1865207 0.08079875 0.02243240
>>#3 18.55295 0.08482219 0.1996013 0.09569044 0.03361565
>>#4 18.55295 0.08482219 0.1996013 0.09569044 0.03361565
>>#5 18.55295 0.19596330 0.1996013 0.04906038 0.03361565
>>#6 18.55295 0.19596330 0.1996013 0.04906038 0.03361565
>>
>>A.K.
>>
>>
>>
>>
>>
>>
>>________________________________
>>From: Joanna Zhang <zjoanna2013 at gmail.com>
>>To: arun <smartpink111 at yahoo.com>
>>Sent: Monday, March 25, 2013 11:19 AM
>>Subject: Read text file in R
>>
>>
>>
>>Hi Arun,
>>
>>I just sent  you a text file via R, but I think it is being held for moderator approval. I attached the final output file here, could you help me read it in R when you have time?
>>
>

______________________________________________
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