R2-D2 has an intact memory of the events of the prequels. He should be aware of what Yoda looks like, and probably has met him.
So why did R2-D2 attack Yoda?
Science Fiction & Fantasy Stack Exchange is a question and answer site for science fiction and fantasy enthusiasts. It only takes a minute to sign up.Sign up to join this community
I can't recall R2-D2 ever meeting Yoda in the prequel movies, and I haven't read many novels set in the prequel-era. Therefore I cannot be certain R2 has ever actually seen Yoda. (EDIT: Though I didn't recall it at the time, R2 has been present with Yoda, during the birth scene in Revenge of the Sith. I'm leaving the rest of my statement unchanged, for posterity.)
That said, I'm going to continue my answer with the assumption that R2 HAS seen Yoda, but I have no evidence for this. Occam's Razor would suggest the answer is, "R2 never saw Yoda"...but that wouldn't be as much fun.
R2, when he met Yoda again on Dagobah, was somewhat irate. Yoda had blinded Luke's instruments, causing them to crash into the swamp. Yoda put R2 through the whole 'travel through the water and get eaten' thing...not R2's happiest moment.
That said, R2 knew it was for Luke's benefit - Yoda was trying to teach. If R2 didn't act as if he wasn't aware of who Yoda was, it could have lead to some uncomfortable questions and/or undermined Yoda's efforts.
So, they played for a bit - R2 gave gentle rebukes (Yoda held Dooku's force lightning in his palm, the arc welder was gentle by that standard) and Yoda played the silly little native for a bit. Inside, they were both grinning.
I like Jeff's answer, but I thought I'd point out some other potential explanations:
It's reasonable to assume that a droid's memory is limited, and the movies make it sound like wiping a droid's memory is a frequent thing. Based on that, maybe R2 periodically "cleans up" his memory and removes the least important things. It's possible that over the 20-ish years between the movies, remembering what Yoda looked like was less important than remembering something else. For example, if R2 thought Yoda was dead, being able to recognize him would be pretty useless. It's also possible that he doesn't have control over his memory clean-up algorithm, and things over a certain age are automatically deleted. He is designed to be a mechanic -- recognizing people he hasn't seen in 20+ years wouldn't be that useful.
R2 is frequently show to be fairly smart in the new movies. It's possible the Empire has a way to extract information from droids. R2 may have erased his own memories related to Yoda in case they contained information that could be used against the Rebellion. He's not designed to be a spy, so it's reasonable that he just deleted all of his memories of Yoda because he wasn't sure which ones were dangerous.
Just because his memory isn't erased at the end of the new movies doesn't mean it wasn't erased at any point after that. R2 and C3-PO seem to be involved in spy operations, so it wouldn't be surprising if someone else erased their memories (either the Rebellion because they have sensitive information, or the Empire because hey, free droids).
And of course, the real explanation for any plot point in the new Star Wars:
According to the official Star Wars Facebook page, they were acting as part of Yoda's test for Luke.
It's not exactly confirmed canon, but it apparently is the theory that some Lucasfilm employees subscribe to.
The simple (albeit hypothetical) answer is that R2-D2 was fed up with Yoda's antics in that particular sequence.
It's easy to assume that R2 was merely guarding Luke's equipment when Yoda first reveals himself in ESB given the context. Luke saying, "Give that back," and R2 attempting to reach for whatever was in Yoda's hand gives the impression that R2 was acting on behalf of Luke's interests.
Let's remember that there are several instances in which Yoda and R2 appear together in the prequels.
Additionally, they are both present on Naboo in the final scene before the credits during the Freedom Day celebration. R2 stands next to Anakin on Queen Amidala's right side while Yoda stands in front of Mace Windu on Amidala's far left. Star Wars TPM Ending Scene
Given that Yoda possesses force powers and is a Jedi master it is safe to say that he is highly cognitive, and since R2's memory has remained in tact during the entire film series, it is refutable that neither would have memory of the other.
Using their history as a basis of their relationship, it is most reasonable to conjecture that R2's attempt to electrocute Yoda is purely circumstantial.
Let's consider Yoda's personality throughout the prequels. There are no instances that come to mind where we see him joking with another character. In fact, there may be no instances during the prequels in which he laughs (can someone confirm this?). Compare his demeanor and actions in the prequels to those in the original trilogy - especially upon first meeting Luke. Some fans argue that he has become senile during his exile on Dagobah which accounts for his mischievousness.
Now consider R2-D2; there are no observable changes to his personality between the prequels and original trilogy. He is just as curt and sassy as he was when we see him interact with C-3PO for the first time in Episode I.
Now, circling back to the question at hand: Why does R2-D2 try to electrocute Yoda?
During the crash landing on Dagobah, R2 was swallowed by a swamp creature and spat out shortly before his encounter with Yoda. Given R2's personality, we can assume that he becomes "annoyed" after this incident. Sure, it's arguable that astromech droids are capable of feeling annoyed in the first place, but his interactions with C-3PO after crash landing on Tatooine in Episode IV prove that it is quite possible. They split up as a result of being annoyed with one another.
The theory that R2 was just "playing along" with Yoda is illogical. R2-D2's nature as a droid and his loyalty to his master, in this case Luke, surely prevent him from putting on a ruse. After all, he is programmed to receive input and perform tasks, how could he know of Yoda's attempt to assess Luke's readiness without explicitly receiving such a command from Yoda himself? Furthermore, there are no instances in the prequel or the original trilogy that come to mind where R2 is "playing along." Take for instance, in ROTJ when Luke gifts R2 and 3PO to Jabba the Hutt. It is revealed later on that R2 was a part of Luke's plan to aid in Han Solo's escape when he throws Luke his lightsaber from the deck of Jabba's sail barge.
It is only reasonable to say that R2-D2 and Yoda knew of each other during their encounter on Dagobah, and that despite their memory of one another, their history throughout the film series has no bearing on their actions in that particular sequence.
R2-D2 was simply not in a mood to deal with Yoda's playfulness, and since Yoda did not immediately reveal his identity to Luke, R2's annoyance was exacerbated.
And this is just another fan's theory...
I don't believe R2 ever tried to attack Yoda. As I recall the scene, R2 was trying to get back that lamp Yoda took from Luke's emergency supplies. R2 grabbed at it, Yoda started hitting R2 with his cane ("Mine Mine or I help you not!") Luke, exasperated, finally told R2 to just let him take it (Yoda was testing Luke to see if he patience to become a Jedi). With all the editing and revamping George likes to do, this might have changed though.
R2 zaps Yoda cause he's an astromech droid and doesn't give a hoot about Jedi Masters. And of course he remembers who Yoda is; how do you think that given the entire planet of Dagobah R2 manages to navigate to exactly the spot where Yoda is? Dagobah is teeming with life so you can't just scan for life-forms, you'd have to know to scan for a 13kg, 66cm tall 38°C life-form with a 25bpm heart rate and a net metabolic output of 600 Watts; these are the kind of things astromech droids care about and that R2 would remember about Yoda.