在 Mathematica 中读取此类文件
这是数据文件:
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
我应该能够访问此文件中的每个元素,我尝试使用此函数在 Mathematica 中创建记录,但我收到错误
ReadList["testA.txt", Number, RecordLists -> true]
Error: ReadList::opttf: Value of option RecordLists -> true should be True or False.
另外,在完成记录后如何访问每个元素?
Mathematica 中还有一种方法可以再创建一个列,该列在两个日期之间存在差异并将其放入新列中。
这个作业确实允许使用 Excel 进行计算,但我必须在 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将
Import
与"Table 一起使用"
格式,甚至可以忽略标题行:You could instead use
Import
with the"Table"
format, which can even ignore the header lines:你问了3个问题,我会尽力回答所有问题。正如 belisarius 指出的,Mathematica 区分大小写。因此,您的代码应该是:
但是,这仍然会生成错误,因为您的第一行由
String
而不是Number
组成。因此,最简单的方法就是采用 Michael Pilat 的解决方案 并使用导入
。这将返回一个列表列表,其中文件中的每个记录都成为子列表之一。要访问特定子列表,请使用
Part
,或其更简单的形式[[ ]]
,如下所示:或者,如果您想要特定列
现在,要将另一列添加到列表中,有几个的方式。最简单的方法是
转置
您的数据,选择现在的行并
对它们应用
函数,将新行附加到旧数据,然后转回
概念上更困难,但更直接的方法是使用
Map
将函数应用于原始数据中的每一行,返回包含新数据的行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:
However, this will still generate an error as your first line is made up of
String
s notNumber
s. So, the simplest thing to do is to go with Michael Pilat's solution and useImport
. 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:Or, if you want a specific column
Now, to add another column to your list, there are a couple of ways. The simplest way is to
Transpose
your data,select the now rows and
Apply
the function to them,attach the new row to the old data, and transpose back
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正确正确:))
Mathematica 对Case-S敏感
True :))
Mathematica is Case-Sensitive