Quote Hanakofuroshira (

)
This only apparently works for an ideal gas.
Yep, this is the correct formula, and is derived through the ideal gas law. It works for most planetary atmospheres you are likely to find -- the law breaks down when the molecules are very large or near phase transitions (like liquid or superfluids).
The average molar mass of a solid planet is much harder to find without knowing the composition first.
Dividing the volume of the planet by its mass (edit: how about the other way around, silly me) gives you its density, but then going from density to molar mass is not straightforward. There are multiple compositions that could produce it, and it further depends on the extent to which the planet is compacted by its own gravity.
We could instead use the mass-radius relationship for solid planets, determining the bulk composition from the planet's mass and radius together, and then breaking that down to average molar mass. But this is only approximate, since the mass-radius relationship is not a very sensitive measure of composition.