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!

有帮助吗?

解决方案

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[])

其他提示

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

(*
-> {1, 7/2}
*)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top