Message Boards Message Boards

1
|
5774 Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Remove elements with a specific value?

Posted 4 years ago

Hello everyone,

I have a dataset: Suicde Data from 1985 until 2016.

For the year 2016 there have been not so many sufficient records so I want to delete all entries from year 2016. Which means in the excel file I want to get rid of all rows where the first column has the value 2016.

OriginalData = 
  Import[FileNameJoin[{NotebookDirectory[], "suicide.xlsx"}]][[1]];

Export[FileNameJoin[{NotebookDirectory[], "suicide.mx"}], OriginalData]

suicide2 = 
 Import[FileNameJoin[{NotebookDirectory[], 
    "suicide.mx"}]];
suicide = Drop[suicide2[All], "2016"] (*With this command I intended to remove the lines from the data "suicide2", but it did not work.*)

Could you please let me know how to remove all entries from the year 2016? I have attached the xlsx file.

Thank you very much in advance!

Attachments:
POSTED BY: Tester Trying
5 Replies
Posted 4 years ago

Change Drop[suicide2[All], "2016"] to Select[suicide2, #[[2]] != 2016 &] or DeleteCases[suicide2, {x_, 2016., y___}] like Hans suggested. Both will do the same thing from a different perspective.

POSTED BY: Sean Cheren

I had to change the the fileformat (converted it to .xls). Then

OriginalData = Import["C:\Users\User\Desktop\suicide.xls"];

and

test = Take[OriginalData[[1]], 50];
test // TableForm
test1 = DeleteCases[test, {x_, 1988., y___}];
test1 // TableForm

Is it this what you are looking for?

POSTED BY: Hans Dolhaine
Posted 4 years ago

Hey Hans,

thank you very much for your response. I tried your command but it did not work and I can not really recall what it should execute. I wish to delete all rows where the value is 2016 in column 2. :)

Thank you very much!

POSTED BY: Tester Trying
Posted 4 years ago

I recommend using {"Data", 1} to select the first sheet as it is more efficient than taking the first part after importing everything. Then, you want to select each row where the second position is not 2016 (its a number, not a string).

In[6]:= data = Import["~/Downloads/suicide.xlsx", {"Data", 1}];

In[12]:= data // Length
Out[12]= 27821

In[13]:= Select[data, #[[2]] != 2016 &] // Length
Out[13]= 27661
POSTED BY: Sean Cheren
Posted 4 years ago

Hey Sean,

thank you very much for that hint! I want all rows to be deleted where the value year (second column) is 2016.

Thanks a lot :)

POSTED BY: Tester Trying
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract