What is the best way (simplest, fastest) to convert the numerical columns of a dataset into a matrix? The numerical columns can have missing values.
For example for this dataset:
ds = Dataset@{<|"passengerClass" -> "3rd", "passengerAge" -> 36.5`,
"passengerSex" -> "male", "passengerSurvival" -> "survived",
"passengerWeight" -> 186.5`|>, <|"passengerClass" -> "3rd",
"passengerAge" -> 9.`, "passengerSex" -> "female",
"passengerSurvival" -> "survived",
"passengerWeight" -> 90.`|>, <|"passengerClass" -> "1st",
"passengerAge" -> 35.`, "passengerSex" -> "male",
"passengerSurvival" -> "survived",
"passengerWeight" -> Missing[]|>, <|"passengerClass" -> "1st",
"passengerAge" -> 60.`, "passengerSex" -> "male",
"passengerSurvival" -> "survived",
"passengerWeight" -> 160.`|>, <|"passengerClass" -> "3rd",
"passengerAge" -> 23.`, "passengerSex" -> "female",
"passengerSurvival" -> "survived",
"passengerWeight" -> Missing[]|>, <|"passengerClass" -> "3rd",
"passengerAge" -> 22.`, "passengerSex" -> "female",
"passengerSurvival" -> "survived",
"passengerWeight" -> 122.`|>, <|"passengerClass" -> "3rd",
"passengerAge" -> Missing[], "passengerSex" -> "female",
"passengerSurvival" -> "survived",
"passengerWeight" -> 120|>, <|"passengerClass" -> "3rd",
"passengerAge" -> Missing[], "passengerSex" -> "female",
"passengerSurvival" -> "survived",
"passengerWeight" -> Missing[]|>, <|"passengerClass" -> "1st",
"passengerAge" -> 17.`, "passengerSex" -> "female",
"passengerSurvival" -> "survived", "passengerWeight" -> 170.`|>}
This does not work:
ds[All, Select[NumberQ]][Values]
Some clarifying points follow.
I would like the
_Missing
entries to be replaced with $0$'s.I do not know in advance the columns of interest. I want to pick the numerical columns of the dataset and then make a matrix with them.
(If I knew how to replace the missing values in a dataset with $0$s I would have solved this problem already.)
Query[Values, Select[NumericQ]][ds]
or your expression withNumericQ
instead ofNumberQ
. $\endgroup$Missing
to a number to make a complete matrix. $\endgroup$ds /. _Missing->0
? $\endgroup$Query[All, Values@*Select[NumberQ]]@ ReplaceAll[Normal[ds2], _Missing -> 0]
. $\endgroup$