3
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ Could replace Null with Nothing, among other possibilities. $\endgroup$ Jul 11 '21 at 15:42
4
$\begingroup$

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} *)
$\endgroup$
3
  • 2
    $\begingroup$ 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 $\endgroup$
    – Lukas Lang
    Jul 11 '21 at 11:18
  • $\begingroup$ 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. $\endgroup$
    – Pedro
    Jul 18 '21 at 10:00
  • $\begingroup$ @Pedro, I have expanded my answer with different possible ways of getting what you want. $\endgroup$
    – Domen
    Jul 18 '21 at 11:01

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.