Timeline for How do I add the nth element for every line in a text file?
Current License: CC BY-SA 3.0
10 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 11, 2016 at 20:27 | comment | added | Ihaveaquestion | Perhaps the syntax is different on Mac? This is what I get when I run Total@data[[All,2]] imgur.com/WEv3YhY | |
May 11, 2016 at 20:15 | comment | added | Ihaveaquestion | Also tried the And @@ Flatten@Map[NumberQ, data, {2}] and it returned true | |
May 11, 2016 at 20:09 | comment | added | Ihaveaquestion | I checked the import, it seems to be working fine. Getting the error "part 2 of {} does not exist" Also, added in what I have in mathematica right now into original question | |
May 11, 2016 at 20:03 | comment | added | MarcoB |
@Ihaveaquestion It seems to work fine with the new file contents too: results. Can you try running data=Import["textfile", "Table"] WITHOUT the semicolon, to make sure that data is being imported correctly? Also try And @@ Flatten@Map[NumberQ, data, {2}] , which should return True if all elements of your file have been correctly imported as numbers.
|
|
May 11, 2016 at 19:57 | comment | added | Ihaveaquestion | replaced original question's lines with 3 actual lines from my file | |
May 11, 2016 at 19:56 | comment | added | MarcoB | @Ihaveaquestion Then perhaps you could show the first few here, just to make sure of format, then provide a link to the whole file (pastebin, filetea, dropbox, github, etc). | |
May 11, 2016 at 19:55 | comment | added | Ihaveaquestion | There are 10k lines in my text file, so I don't think I can fit that here. The elements in each line are separated by a single space, and each line has 13 elements. | |
May 11, 2016 at 19:55 | comment | added | MarcoB | @Ihaveaquestion Well, I took your sample data and copied in a text file, then ran the code exactly as shown, just substituting the correct path, and obtained these results. I suspect that perhaps the format of the file is not as you expect it, or the numbers are not stored in a simple form. Could you share the exact contents of your data file? | |
May 11, 2016 at 19:53 | comment | added | Ihaveaquestion | I believe I did everything you mentioned, but it doesn't produce an output. data=Import["textfile", "Table"]; Total@data[[2;;10000,2]]; (If I wanted to add up everything in the second column) | |
May 11, 2016 at 19:46 | history | answered | MarcoB | CC BY-SA 3.0 |