[From nobody Sat Mar 18 16:55:25 2006 Message-ID: <430C4822.40104@tuebingen.mpg.de> Date: Wed, 24 Aug 2005 12:12:50 +0200 From: Jan Wiener <jan.wiener@tuebingen.mpg.de> User-Agent: Mozilla Thunderbird 1.0.6 (X11/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Prof Brian Ripley <ripley@stats.ox.ac.uk> Subject: Re: [R] reading "special" text files References: <430C345F.1050406@tuebingen.mpg.de> <Pine.LNX.4.61.0508241056500.8536@gannet.stats> In-Reply-To: <Pine.LNX.4.61.0508241056500.8536@gannet.stats> Content-Type: multipart/mixed; boundary="------------060806010401010509090000" This is a multi-part message in MIME format. --------------060806010401010509090000 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit well, the problem is that my txt file looks like this 1 1 2 4 1 2 3 4 6 ,i.e, I have different amount of entries in each line, and I have spaces somewhere in each line. I would like to have a R data structure that converts this input file into 1 1 2 4 1 2 3 4 6 if I use read.table with the input file attached (read.table("foo.txt", header=FALSE, fill = TRUE)), as you suggested, I get a data structure that is for some reason restricted to 10 columns (and that has more lines than the input file). Furthermore, I get NA's in the data structure. This would be fine, if there was any way to copy a line of the data structure into a vector without copying the NA? greetings, jan Prof Brian Ripley wrote: > On Wed, 24 Aug 2005, Jan Wiener wrote: > > >>hello, >> >>assume you have a txt-file (sep="\t" or "") looking as follows that you >>want to read into a variable: > > > Into a variable or a data frame? > > >>1 1 2 3 >>2 1 2 3 4 >>3 1 5 6 7 8 >> >>the result should look like this (e.g., in a data.frame): > > > With no column names? Not possible. > > >>1 1 2 3 >>2 1 2 3 4 >>3 1 5 6 7 8 >> >>is there any way to do so? > > > I think we need to know what the structure here is. At a guess, the > separator is multiple tabs as in the example. In that case > > >>read.table("foo", header=FALSE, fill = TRUE) > > V1 V2 V3 V4 V5 V6 > 1 1 1 2 3 NA NA > 2 2 1 2 3 4 NA > 3 3 1 5 6 7 8 > > or > > >>read.table("foo", header=FALSE, fill = TRUE, row.names=1) > > V2 V3 V4 V5 V6 > 1 1 2 3 NA NA > 2 1 2 3 4 NA > 3 1 5 6 7 8 > > does in essence what you want. > > -- Dr. Jan Malte Wiener Max-Planck-Institute for Biological Cybernetics Spemannstr. 38, 72076 Tuebingen, Germany tel.: +49 7071 601 631 email: jan.wiener@tuebingen.mpg.de url: www.kyb.tuebingen.mpg.de/~malte --------------060806010401010509090000 Content-Type: text/plain; name="foo.txt" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="foo.txt" 1 38 3 16 22 17 19 3 2 26 15 18 23 17 8 15 3 29 21 7 14 15 20 19 21 4 23 6 21 22 10 9 4 6 5 44 4 19 24 23 18 6 1 4 6 11 25 9 5 4 8 10 22 25 7 30 11 18 24 8 9 4 3 10 11 8 16 5 2 1 6 7 19 4 20 5 9 39 23 15 10 5 4 9 14 6 12 23 10 18 24 22 17 21 16 11 14 9 15 24 11 37 20 23 18 17 8 3 4 5 9 10 20 12 14 21 6 1 2 7 8 3 4 20 24 21 101 5 16 6 1 2 7 19 18 23 16 102 2 1 11 16 21 22 17 12 4 3 1 103 9 3 14 15 20 25 24 19 17 11 12 3 13 40 3 11 22 19 10 3 14 24 6 4 15 24 17 6 15 43 11 2 3 14 23 16 11 16 17 21 24 15 4 1 12 21 17 45 22 16 2 8 15 19 23 22 18 27 25 14 9 8 2 6 16 25 19 35 5 2 1 11 16 18 25 15 5 20 10 24 15 10 3 18 17 7 11 24 21 28 20 19 24 22 12 6 2 4 9 20 22 21 3 8 12 22 23 19 20 15 11 3 23 31 25 20 21 12 1 2 9 5 10 14 25 24 12 23 24 25 20 9 7 6 11 16 17 23 201 1 15 4 1 12 16 22 24 19 15 202 6 1 4 10 14 19 22 16 12 7 1 203 8 10 20 24 18 16 11 1 2 4 8 10 25 41 3 16 23 25 13 3 26 15 1 21 13 19 10 1 27 32 21 2 3 13 14 24 21 28 20 11 21 13 10 4 7 11 29 33 23 16 7 13 5 10 19 23 30 13 4 8 1 11 12 24 20 4 31 42 16 25 13 8 9 1 12 19 16 32 19 5 9 2 16 22 18 13 20 5 33 36 1 11 22 18 25 5 13 8 2 1 34 22 3 2 12 21 18 20 14 5 9 3 35 34 24 19 5 3 1 6 7 12 21 18 24 36 25 23 22 16 6 2 8 5 10 19 14 23 301 4 15 25 23 22 13 14 4 5 15 302 3 6 12 21 18 19 15 10 9 3 6 303 7 25 24 22 21 11 12 18 13 9 15 25 --------------060806010401010509090000-- ]