“We use for + the -ing form of a verb to talk about the function of something or how something is used.”
This dictum from English Grammar Today might have two very different meanings:
“To talk about the function of something or how something is used we must use for + the -ing form of a verb.”
This is simply wrong. There are other ways of talking about these functions, even if we restrict ourselves to the constructions licensed by the verbs use and need: a marked infinitive, a subordinate clause introduced by in order that or so that or to the end of or for the purpose of. In fact, the quotation itself uses a marked infinitive with use for exactly that purpose: to talk about the function of, &c.
“A use of *for + the -ing form of a verb is to talk about the function of something or how something is used.”
This is quite a different matter. This talks about what the construction is good for—or one of the things it is good for, since it has other uses, such as specifying the reasons for an award or an arrest.
Context, context, context!
There is a subtle difference, but it's not due to something being a single event or not.
I prefer to have a cup of coffee (this time).
This isn't talking about a particular event because of to have, it's a particular event because of the use of a in the sentence construction.
Contrast it with the following:
I prefer to have single cups of coffee (in general).
Now, it's no longer a single event. It's talking about an ongoing state of affairs and intentions.
So, let's compare those two sentences:
I prefer having single cups of coffee.
I prefer to have single cups of coffee.
The only real difference between the two, which is often not really thought about, is that the former talks more about those times during which a single cup of coffee is in the process of being had. Meanwhile, the latter talks more about the nature of how the coffee will be had.
It's a subtle distinction. In fact, most people would probably frame the statement differently, bypassing the distinction altogether:
I prefer single cups of coffee.
A similar analysis could be performed on your first sentence, although I need to rephrase it slightly:
I prefer swimming to running.
I would rather swim than run.
I would say that the distinction here is even more subtle and less present. But if it's to be made at all, it would again be that the first sentence talks more about being currently engaged in the process of the activity (perhaps in the context of enjoying it), while the second talks more about the idea of the activities (perhaps theorizing about their benefits).
In both cases, however, there is really no effective difference between the two. They are often used interchangeably.
Best Answer
Your intuition is right - "prefer to study at X to to study at Y" is no good.
Your suggestion isn't bad, but it does sound a bit unnatural. "prefer ... rather than" is sort of strange considering that "rather" itself often means "prefer".
This usually expresses a preference for a specific event though rather than your general preferences. For alternatives so you can use the infinitive in the first clause and keep the expression general, use direct expressions of comparison (all of which work with both the infinitive and the gerund):