문제

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