0
$\begingroup$

I'm trying to import a file with 2 columns, in TSV format.

Something like this:

time distance
1    25
2    43
3    43
4    54
5    65
6    67 
7    68
8    23
9    99
10   35

And I want to do certain things to column 2, such as multiply by 2 or do the difference between the elements.

So, I was trying something like:

testing = Import["timedistance.dat"]//TableForm

testing[All, 2]

And "testing[All, 2]" gives me the following error: "Part::partw: Part 2 of {}"

What am I doing wrong?

$\endgroup$
7
  • $\begingroup$ //TableForm puts the value of testing as a displayed table and not an array. $\endgroup$
    – K7PEH
    Mar 8 '15 at 16:09
  • $\begingroup$ removing //TableForm and doing "testing = Import["timedistance.dat", "Table"]" gives me the same error. How do I fix it? $\endgroup$
    – MattiasDam
    Mar 8 '15 at 16:18
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Mar 8 '15 at 16:18
  • $\begingroup$ Import["timedistance.dat"] all by itself. Then, consider something like Transpose[testing][[All]][[2]]. $\endgroup$
    – K7PEH
    Mar 8 '15 at 16:19
  • $\begingroup$ Doesn't work also. Gives two errors: 1. Transpose::nmtx : The first two levels of the one-dimensional list cannot be transposed. 2. Part:partw: Part 2 of transpose does not exist. $\endgroup$
    – MattiasDam
    Mar 8 '15 at 16:37
1
$\begingroup$

Take a look at how the data is imported first. Using the data provided in the question.

testing = Import["sample.tsv"]
(*{{"time distance"}, {"1    25"}, {"2    43"}, {"3    43"}, {"4    \
54"}, {"5    65"}, {"6    67 "}, {"7    68"}, {"8    23"}, {"9    \
99"}, {"10   35"}}*)

You can see that the data is imported as a single text per record. Importing with the "table" option.

testing = Import["sample.tsv", "Table"]
(*{{"time", "distance"}, {1, 25}, {2, 43}, {3, 43}, {4, 54}, {5, 
  65}, {6, 67}, {7, 68}, {8, 23}, {9, 99}, {10, 35}}*)

Now the data is amenable for computation.

title = First@testing; testing=Rest@testing;
{#[[1]],  #[[2]]^2} & /@ testing
(*{{1, 625}, {2, 1849}, {3, 1849}, {4, 2916}, {5, 4225}, {6, 4489}, {7, 
  4624}, {8, 529}, {9, 9801}, {10, 1225}}*)

Using version 10 functionality

testing = SemanticImport["sample.tsv"]

enter image description here

ds[All, Append[#, "function1" -> (2 #distance)] &]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.