Mean per group in a data.frame

This type of operation is exactly what aggregate was designed for: Here we aggregate columns 3 and 4 of data.frame d, grouping by d$Name, and applying the mean function. Or, using a formula interface:

R “Error: unexpected ‘}’ in “}”

Your code has a number of issues: The if statement would need enclosing {} symbols. The if also needs enclosing () symbols around the logical expression. The result of the recode function is not stored anywhere (the function does not change values-in-place). Loops are a poor solution to this problem. It would be much simpler to take advantage of R’s natural vectorization. Rather than an if inside … Read more

Insert picture/table in R Markdown

Several sites provide reasonable cheat sheets or HOWTOs for tables and images. Top on my list are: Pandoc readme, specifically tables RStudio’s RMarkdown, more details in basics (including tables) and a rewrite of pandoc’s markdown. Pictures are very simple to use but do not offer the ability to adjust the image to fit the page (see Update, below). To adjust the image … Read more

Initial value in ‘vmmin’ is not finite even when changing the starting value

tl;dr your model needs 4 parameters, but you only gave 3 parameters in the starting vector. I figured this out by (1) trying OLS(c(1,1,1),y=y,z=z) (to confirm that the return value is NA at the starting value); (2) setting debug(OLS) and stepping through it. When you step through the function checking values as you go you can see that sigma2 becomes NA, because k==3 (the model matrix has … Read more