# Removing empty elements

I have data from people being asked for a result of the swab test - (1=pos, 2=neg, 3=not received, 4= inconclusive,9=ignored, empty cell= not applicable). I'm looking for a way to get rid of the not applicable cells. It does not say N/A or anything - they are just empty. So it looks something like this:

{,,,,,,,,,2,,,,,,1,,,,,2,,,,,,,,,,,,,,,,,,,,,,9,,,,,,1} How would I get rid of all empty ones and only keep the numbers?

• Could replace Null with Nothing, among other possibilities. Jul 11 '21 at 15:42

The solution depends on how the empty value are represented. Furthermore, you can use any of the many Mathematica functions, such as DeleteCases, Select, Cases, Replace ...

(* Empty values represented with Null *)
arr = {, , , , , , , , , 2, , , , , , 1, , , , , 2, , , , , , , , , , , , , , , , , , , , , , 9, , , , , , 1};
DeleteCases[arr, Null]
Select[arr, Not[# === Null] &]
Cases[arr, _?NumericQ]
arr /. Null -> Nothing

(* {2, 1, 2, 9, 1} *)

(* Empty values represented with "" *)
arr = {"", "", "", "", "", "", "", "", "", 2, "", "", "", "", "", 1, "", "", "", "", 2, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 9, "", "", "", "", "", 1};

DeleteCases[arr, ""]
Select[arr, # != "" &]
Cases[arr, _?NumericQ]
arr /. "" -> Nothing

(* {2, 1, 2, 9, 1} *)

• Depending on where the data is coming from and how it is displayed, it might also be filled with "" (i.e. empty strings). But only OP knows which one is true of course Jul 11 '21 at 11:18
• Thank you. Unfortunately, not sure why that did not delete the empty ones. I instead of getting rid of empty ones I chose to go with Select[swabTest,NumberQ].. This then gave me only the ones with results in it. Jul 18 '21 at 10:00
• @Pedro, I have expanded my answer with different possible ways of getting what you want. Jul 18 '21 at 11:01