Skip to content

converting an ASCII file to a matrix

3 messages · Michael Graber, Uwe Ligges

#
Dear R-WinEdit users,

I have a simple question, but somehow I cannot find the answer even
though I tried a lot!

I have an unstructured ASCII-file and I want to import it into a matrix
m in R, so that every character is defined by m[i;j]. The rows are not
of the same length.

The file looks like the following shortened abstract example:

name: xxxxx xxxx
age: 9.9.99
record number: 999
title: xxxxx xxxx xxx
keywords: xxx xx

"white space"

name: yyyy yyyyyyyyyyyy
age: 8.8.88
record number: 8
title: yyyy yy yyyy
keywords: yyyyyyyyyyy yyyyyyyy yyy

"white space"

The result should be for example: m[1;1]=n
 
I would be very grateful for your help!

Michael Graber
michael.graber at mail.uni-wuerzburg.de
#
Michael Graber wrote:
a) What is an R-WinEdit user?

b) I guess you mean R-WinEdt (without an i) implying the plug-in for the 
WinEdt editor? WinEdit is another editor that does not support R very 
closely, AFAIK.

c) The following questions are completely unrelated to any editor, so 
why do you ask only a very small (empty?) subset of the R community?
d) What does m[i;j] mean? If we are speaking R, I guess you mean m[i,j]?
So what aboutreading all lines, and storing separate characters as 
vectors in a list using strsplit().
   L <- strsplit(readLines(filename), "")
   L[[i]][j]

A matrix seems to be the wrong way with unequal line lengths.

Uwe Ligges
#
Uwe Ligges wrote:

            
Let me add, what about reading it in using read.dcf(), a function that 
is designed for the data specified above!
And much more appropriate than looking at single characters, I think.

Uwe Ligges