Difference between curly brackets and square brackets

The curly brackets are used to denote a block of code in a function. So, say we need a function to calculate the standard error we might do this.

se <- function(x) {
    n <- length(x)
    sd(x)/sqrt(n)
}

# Showing this in action

x <- rnorm(100, 10, 3)  # Simulate 100 values from a normal distribution with mean 10 and sd 3
se(x)
## [1] 0.2795

The square brackets are used to subset vectors and data frames.


x[1:10]
##  [1]  9.560 12.459 11.047  8.008 10.815 10.872 10.776 10.105 13.013 11.456
x[x > 10]
##  [1] 12.46 11.05 10.82 10.87 10.78 10.10 13.01 11.46 15.09 12.49 11.93
## [12] 18.44 10.98 10.63 11.90 13.53 11.39 11.83 10.68 11.39 11.65 17.19
## [23] 14.49 12.42 12.05 10.45 13.03 15.23 13.75 10.58 12.58 10.50 11.33
## [34] 12.37 12.14 11.22 10.27 11.47 12.31 11.03 15.87 13.04 14.35 14.81
## [45] 10.01 13.71 11.90 14.78 12.27 11.07 12.81 11.60 13.23 10.67 13.99
## [56] 11.89 10.96