1

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.

highBandWidth
  • 16,751
  • 20
  • 84
  • 131
user458858
  • 455
  • 3
  • 6
  • 17
  • BTW, you already asked eight questions, and never accepted an answer ... Is StackOverflow useful for you? If yes, start marking the better answers as "accepted" to show that – Dr. belisarius Nov 03 '10 at 02:34
  • OMG!! You NEVER voted an answer. And NEVER answered a question. It's almost a shame ... – Dr. belisarius Nov 03 '10 at 02:46
  • 1
    belisarius is correct. This site operates on accumulated reputations which is acquired via others voting on the questions and answers you have posted and possible having your answers accepted as the best/correct one. For this system to work, you should vote on the answers you find useful, and accept those answers which best answer your questions. To do otherwise, in the long term you will eventually find your questions being ignored. – rcollyer Nov 03 '10 at 03:29

3 Answers3

10

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}}
Michael Pilat
  • 6,480
  • 27
  • 30
6

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}}
Community
  • 1
  • 1
rcollyer
  • 10,475
  • 4
  • 48
  • 75
  • @belisarius: Thanks. There are some days that it is all I do. – rcollyer Nov 03 '10 at 04:18
  • 2
    With regard to adding columns I recently discovered the joys of `ArrayFlatten`: http://stackoverflow.com/questions/1244782/how-to-prepend-a-column-to-a-matrix/2274679#2274679 – Janus Nov 03 '10 at 04:33
  • @Janus: I tend to forget about that one, but you are correct, it would eliminate any need for the `Transpose`-process-`Transpose` method. – rcollyer Nov 03 '10 at 04:35
4

True :))

Mathematica is Case-Sensitive

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190