I know how to do ordinary random sampling using R:
mysample <- mydata[sample(1:nrow(mydata), 100),]
However, I want to sample by id variables. Let me explain - my dataset looks like this:
id var1 var2 ...
1 5.1 1.2
1 4.7 0.9
2 3.3 1.6
3 3.4 5.7
4 7.9 1.3
Now, I want to take a random sample of, say, 2, by id numbers. Let's say the random sample yields id 1 and 4, then my sample would look like this:
id var1 var2 ...
1 5.1 1.2
1 4.7 0.9
4 7.9 1.3
In other words, I'm sampling 2 id numbers, but I'm actually getting 3 cases.
How can I accomplish this in R?