I've been looking at the ISS's List of International Space Station expeditions, and I'm curious about how the commanders are chosen. I can't really find anything that stands out. Sometimes the commander is military (or former military) and sometimes they're civilian, so military rank doesn't seem to be what decides it. It also doesn't even seem to be that the most experienced astronaut/cosmonaut becomes commander.
For instance, for Expedition 40 Steven R. Swanson was the commander, but Aleksandr Skvortsov went up with him for Expedition 39. Skvortsov had military experience as a leader, he logged more time in space than Swanson, he was a cosmonaut before Swanson became an astronaut, and Skvortsov even had experience as a commander on the ISS during Expedition 24, while Expedition 40 was Swanson's first time aboard.
I can't really find anything that says why a particular person is chosen to be the commander of an Expedition. How are they chosen, and is there any difficulty among countries/agencies when deciding who will be commander?