R for Stata users

Function arguments

Some syntax differences to call functions - Options are separated by spaces in Stata. In contrast, arguments are separated by commas in R - Option abbreviations are pre-defined in Stata. In contrast, argument names can be arbitrary shortened in R as long as there is no ambiguity. To quote Hadley Wickman in Advanced R:

"When calling a function you can specify arguments by position, by complete name, or by partial name. Arguments are matched first by exact name (perfect matching), then by prefix matching, and finally by position"

Valid names

A syntactic name in R consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number.


R loops look similar to Stata loops, albeit with more parenthesis:

Stata R
foreach character in a b c { for (character in c("a", "b", "c")) {
foreach i of numlist 1/100 { for (i in 1:100) {
if 2 > 1 { if (2 > 1) {
else {
} else {

Instead of loops, you may use higher order functions such as lapply (that outputs a list)/ sapply (that simplifies the output in a vector). Use the syntax that looks clearer to you (when looping over elements of a big vector, higher order functions tend to be faster than explicit loops).

Finally, you can run loops in parallel with the foreach package.