Question

I try and perform operations on a set of lists. For example:
Lists: (1, 1, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5), (1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), (1, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA), (1, 1, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5)

Then I would like for example to have a list containing the means. Mean[lists] doesnt run properly because it does not know how to handle the missing values "NA". What I expect is: (1, 1, 5/3, 7/3, 3, 4, 4, etc.)

Does anybody how to ignore the missing or 'text' values? Like you would operate such a function in excel for example. Thanks!

Était-ce utile?

La solution

It looks like you're interested in the mean of the columns of the matrix. If your data array is rectangular you could do something like this

lst = {{1, 1, 1, "NA", "NA"}, {2, 3, 4, 5, "NA"}, {6, 7, 8, 9, 10}};
Mean /@ DeleteCases[Transpose[lst], a_ /; Not[NumericQ[a]], {-1}]

A simpler method would be to replace whatever symbol is used for non-available elements (for example "NA" in lst) by Sequence[]:

Mean /@ (Transpose[lst] /. "NA" -> Sequence[])

Autres conseils

l = {{1, 1, 1, "NA", "NA"}, {2, 3, 4, 5, "NA"}}
Mean@# & /@ (Cases[#, Except@"NA"] & /@ l)

(*
-> {1, 7/2}
*)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top