Computing conditional variance Is there a package in R that does that?
How could I compute the conditional variance $\mathrm{var}(y|x)$ of a sample?
I also don't know what to do in case where there are not enough $y$ for each $x$.
Variance exists but for close $x$, so each $x$ gets only one value hence 
$\mathrm{var}(y|x) = 0$.
 A: In a regression setting, this can be done by first fitting a regression model to the data, and then fitting a second regression model, where the response variable is the squared residuals of the first model.  This works because regression is a method of estimating the conditional mean of the data, and the conditional mean of the squared distance from the mean (i.e. the residuals) is the conditional variance.  This method will result in a biased estimate of the variance (it will be smaller than the true value) because we have estimated the conditional mean from the data.  One possible solution is to fit the second model to the squared leave-one-out residuals of the first model.
Alternatively, you can perform a maximum-likelihood model of both the conditional variance and conditional mean at the same time, but I don't know of any R code for that.
I wrote (with co-authors) a paper on this in a non-linear regression setting a few years back, which may be of some use:
G. C. Cawley, N. L. C. Talbot, R. J. Foxall, S. R. Dorling and D. P. Mandic, Heteroscedastic kernel ridge regression, Neurocomputing, vol. 57, pp 105-124, March 2004. (doi, preprint)
It seems a fairly obvious approach, so I suspect it may have been done before in a different setting.
