1
$\begingroup$

I'm very new at using Mathematica and it's not my Job. It's just something I like.

data = MapAt[DateString[{#, {"Day", "/", "Month", "/", "Year"}}] &,
  Import["D:\\Sheet 1.tsv"][[3 ;;, {1, 2}]]
 , {All, 1}]

I have found out that DateString[{#, {"Day", "/", "Month", "/", "Year"}}] seems to be a function can I define it in a variable and reuse? It would be clearer for me.

Should I write f[x] := DateString[{#, {"Day", "/", "Month", "/", "Year"}}] ?

I'm a c# developer and we would call it "Refactor\Introduce a new variable". May I do the same also for Import["D:\Sheet 1.tsv"]? I guess I can but then what is [[3 ;;, {1, 2}]]?

This are the data which of the example, https://docs.google.com/spreadsheets/d/1dmv_C2_J7uG8zIhxe5aYZbg79MREI8ePsFJTStSKEeE/edit#gid=0

$\endgroup$
7
  • 2
    $\begingroup$ you can use ClearAll[f]; f[x] := DateString[{x, {"Day", "/", "Month", "/", "Year"}}] or ClearAll[f]; f= DateString[{#, {"Day", "/", "Month", "/", "Year"}}]&; $\endgroup$
    – kglr
    Oct 27, 2019 at 15:03
  • $\begingroup$ What the & stands for? $\endgroup$
    – Revious
    Oct 27, 2019 at 15:04
  • 1
    $\begingroup$ Revious, see Function (&) in the docs. $\endgroup$
    – kglr
    Oct 27, 2019 at 15:05
  • $\begingroup$ @kglr: thanks, do you know also where I can find the documentation to understand this piece of code: [[3 ;;, {1, 2}]]? $\endgroup$
    – Revious
    Oct 27, 2019 at 15:06
  • 1
    $\begingroup$ try Part and Span (;;). In general, highlight the [[ or ]] and hit F1 to get the related documentation page. Similarly, highlight ;; and hit F1, highlight & and hit F1 etc. $\endgroup$
    – kglr
    Oct 27, 2019 at 15:18

1 Answer 1

1
$\begingroup$

You can define the function f in several ways

ClearAll[f]; 
f[x_] := DateString[{x, {"Day", "/", "Month", "/", "Year"}}]

or

ClearAll[f]; 
f = DateString[{#, {"Day", "/", "Month", "/", "Year"}}]&

or

ClearAll[f]; 
f = Function[DateString[{#, {"Day", "/", "Month", "/", "Year"}}]]

Import your data:

imp = Import["D:\\Sheet 1.tsv"];

Take rows from the third row to the last (3;;) and columns 1 and 2 ({1,2}) from imp:

data = imp[[3 ;;, {1, 2}]]; 

Map f on entries in column 1:

data = MapAt[f, data, {All, 1}]

See also:

$\endgroup$
5
  • $\begingroup$ just another question. I got an error by doing: ClearAll[f]; ClearAll[file1]; f[x] := DateString[{x, {"Day", "/", "Month", "/", "Year"}}]; file1 = Import["C:\\Users\\Utente\\Documents\\1.csv"]; file1[[2 ;;], {1, 2}] $\endgroup$
    – Revious
    Oct 27, 2019 at 15:32
  • $\begingroup$ it says: Syntax::sntxf: "file1[" cannot be followed by "[2;;],{1,2}]". $\endgroup$
    – Revious
    Oct 27, 2019 at 15:32
  • $\begingroup$ My mistake, I misplaced the , $\endgroup$
    – Revious
    Oct 27, 2019 at 15:39
  • 1
    $\begingroup$ aren't you missing the pattern underscore for the argument to f? I think that should be f[x_]:=... $\endgroup$ Oct 30, 2019 at 22:09
  • $\begingroup$ Thank you @AlbertRetey; corrected the typo. $\endgroup$
    – kglr
    Oct 30, 2019 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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