1
$\begingroup$

I have looked here, in several posts, but I can't solve it

here my list

lista = {-7.54, -4.49, -4.94, -5.87, -7.26, -7.38, -8.3, -7.79, -6.4, -9.58,-6.5, -9.47, -4.2, -4.15, -5.96, -7.47, -6.9, -5.65, -6.03, -7.61,-7., -7.26, -7.82, -8.86, -6.09, -10.4, -9.16, -8.42, -1.7,-7.05,-7.77, -8.63, -7.63, -9.56, -5., -7.01, -8.82, -8.08, -7.12, -8.,-8.42, -4.8, -7.89, -7.,-6.99, -6.85, -7.99, -6.2, -6.93, -4.06,-8.67, -7.53, -8.14, -6.11, -7.5, -6.31, -5.4, -8.67, -9.62,-7.1,-6.61, -7.39, NA, -7.19, -8.45, -6.14, NA, NA, NA, -8.11,-7.88, -6.96, -8.89, -6.92, -6.42, -4.91, -6.22}

I need to remove or filter from a list, ALL the elements "NA" 'cause I needed to calculate statistical data, the database is much bigger than this, it is just a sample. NA isn't a STRING in my list (see the pic)

enter image description here

Any ideas? thanks advance.

$\endgroup$
1
  • $\begingroup$ Cases[lista, Except[NA]] $\endgroup$
    – Syed
    May 16, 2023 at 10:47

1 Answer 1

1
$\begingroup$

You can use Select, Cases, DeleteCases and ReplaceAll as follows:

lista1 = Select[NumericQ] @ lista
{-7.54, -4.49, -4.94, -5.87, -7.26, -7.38, -8.3, -7.79, -6.4, -9.58,
-6.5, -9.47, -4.2, -4.15, -5.96, -7.47, -6.9, -5.65, -6.03, -7.61, 
-7., -7.26, -7.82, -8.86, -6.09, -10.4, -9.16, -8.42, -1.7, -7.05, 
-7.77, -8.63, -7.63, -9.56, -5., -7.01, -8.82, -8.08, -7.12, -8., 
-8.42, -4.8, -7.89, -7., -6.99, -6.85, -7.99, -6.2, -6.93, -4.06, 
-8.67, -7.53, -8.14, -6.11, -7.5, -6.31, -5.4, -8.67, -9.62, -7.1, 
-6.61, -7.39, -7.19, -8.45, -6.14, -8.11, -7.88, -6.96, -8.89, -6.92, 
-6.42, -4.91, -6.22}
lista2 = DeleteCases[NA] @ lista;

lista3 = Cases[_?NumericQ] @ lista;

lista4 = ReplaceAll[NA -> Nothing] @ lista;


lista1 == lista2 == lista3 == lista4
 True
$\endgroup$
5
  • $\begingroup$ How would this be in this case? with NA elemets, I'm new to using math, btw thanks again..... lista = {{Pal_1, 130.067, 19.023, 13.7, 20.3, -1.01, -2.54, 0.6, 19.9, NA, 7.1, 2.37, 0.74, 2.9, 16.76, 0.15, 13.62, 12.36}, {NGC_1466, 286.7, -39.537, 39.4, 38.4, -2.15, -7.54, 2.8, 73.9, NA, 8.82, 2.48, 3.69, 14.3, 18.8, 0.07, 10.66, 10.23}} $\endgroup$
    – xg3istx
    Dec 16, 2020 at 23:11
  • $\begingroup$ For list of lists as input, you can use Select[NumericQ] /@ lista or Cases[_?NumericQ] /@ lista or DeleteCases[Except[_?NumericQ]] /@ lista. $\endgroup$
    – kglr
    Dec 16, 2020 at 23:20
  • $\begingroup$ lista = {{12.06,10.77} , {7.33,NA} , {6.34,4.21}} I need remove only {7.33,NA} .. I just need this on my list lista = {{12.06,10.77} , {6.34,4.21}} @kglr, can you help?, this is a nightmare for me $\endgroup$
    – xg3istx
    Dec 19, 2020 at 19:11
  • 1
    $\begingroup$ try DeleteCases[{_,NA}]@lista or Select[FreeQ[NA]]@lista $\endgroup$
    – kglr
    Dec 19, 2020 at 22:00
  • $\begingroup$ thanks again amte $\endgroup$
    – xg3istx
    Dec 20, 2020 at 14:55

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.