Has a sitting president ever been defeated in the primaries?
I think Franklin Pierce, in favor of James Buchanan, qualifies:
http://en.wikipedia.org/wiki/Franklin_Pierce
In individual primaries, yes, but in the modern era not enough were won to take the nomination away from the incumbent.
Kennedy won several primaries against Carter in individual states.
That said, McCarthy just got close to Johnson in New Hampshire and scared him out of the race. Right after New Hampshire Robert Kennedy got in the race and reportedly Johnson's poll numbers fell through the floor, so he pulled out.