user458858
user458858

Reputation: 455

Reading this type of file in Mathematica

This is the data file:

ID  YR  MO  DA  YrM  MoM  DaM  
100  2010  2  20  2010  8  30  
110  2010  4  30  2010  9  12     
112  2010  8  20  2010  10  28  

I should be able to access each element in this file, i tried to use this function in creating record in Mathematica but i am getting a error

ReadList["testA.txt", Number, RecordLists -> true]

Error: ReadList::opttf: Value of option RecordLists -> true should be True or False.

Also how do I access each element after doing the records?

Also is there a way in Mathematica to create one more column which does difference between two dates and put it in new column.

This homework assignment does allow to use excel to compute, but i have to do this in Mathematica.

Upvotes: 1

Views: 1647

Answers (3)

rcollyer
rcollyer

Reputation: 10685

You've asked 3 questions, and I'll try to answer them all. As belisarius pointed out, Mathematica is case sensitive. So, your code should be:

In[1]:=ReadList["testA.txt", Number, RecordLists -> True]

However, this will still generate an error as your first line is made up of Strings not Numbers. So, the simplest thing to do is to go with Michael Pilat's solution and use Import. This returns a list of lists where each record in the file becomes one of the sublists.

To access a specific sublist, you use Part, or its simpler form [[ ]], as follows:

In[2]:={{100, 2010, 2, 20, 2010, 8, 30}, 
        {110, 2010, 4, 30, 2010, 9,12}, 
        {112, 2010, 8, 20, 2010, 10, 28}}[[1]]
Out[2]:={100, 2010, 2, 20, 2010, 8, 30}

Or, if you want a specific column

In[3]:={{100, 2010, 2, 20, 2010, 8, 30}, 
        {110, 2010, 4, 30, 2010, 9,12}, 
        {112, 2010, 8, 20, 2010, 10, 28}}[[All,4]]
Out[3]:={20, 30, 20}

Now, to add another column to your list, there are a couple of ways. The simplest way is to Transpose your data,

In[4]:=Transpose[data]
Out[4]:={{100, 110, 112}, {2010, 2010, 2010}, {2, 4, 8}, 
         {20, 30, 20}, {2010, 2010, 2010}, {8, 9, 10}, {30, 12, 28}}

select the now rows and Apply the function to them,

In[5]:=Plus @@ Out[4][[{3,6}]]
Out[5]:={10,13,18}

attach the new row to the old data, and transpose back

In[6]:=Out[4]~Join~Out[5] // Transpose
Out[6]:={100, 2010, 2, 20, 2010, 8, 30, 10}, 
        {110, 2010, 4, 30, 2010, 9, 12, 13}, 
        {112, 2010, 8, 20, 2010, 10, 28, 18}}

A conceptually more difficult, but more straightforward method is to use Map to apply a function to each row in the original data that returns the row with the new datum present

In[7]:=Map[#~Join~{Plus@@#[[{3,6}]]}&, data]
Out[7]:={100, 2010, 2, 20, 2010, 8, 30, 10}, 
        {110, 2010, 4, 30, 2010, 9, 12, 13}, 
        {112, 2010, 8, 20, 2010, 10, 28, 18}}

Upvotes: 6

Michael Pilat
Michael Pilat

Reputation: 6520

You could instead use Import with the "Table" format, which can even ignore the header lines:

In[1:= Import["test.txt", "Table", "HeaderLines" -> 1]

Out[1]= {{100, 2010, 2, 20, 2010, 8, 30}, {110, 2010, 4, 30, 2010, 9,
12}, {112, 2010, 8, 20, 2010, 10, 28}}

Upvotes: 10

Dr. belisarius
Dr. belisarius

Reputation: 61016

True :))

Mathematica is Case-Sensitive

Upvotes: 4

Related Questions