# R is easy to use as a pocket calculator:
3 + 5       
4 / 3           
g <- 20             
g                               
10 + g
# You can build vectors, and access them as a whole, or only selected elements:
a <- c(2,3,9,1,6,7)       
a
# We can refer to one element, several of them, or all of them except some, by giving as subindices numerical vectors:
a[1]      
a[c(1,3)]
a[-3]                     
# We can also give as indices logical vectors, a technique useful to recover all elements in a vector that verify a certain property:
a[a > 3]
a > 3
# The rule is that operations are performed element by element on similarly sized objects; for ordinary matrix products, see below.
b <- a*a                        # Elementwise product.
b
d <- matrix(a,2,3,byrow=T)      # It is easy to create matrices,
d
d[2,2]                          # and reference their elements.
d[2,c(1,2)]
d[,1]                           # It an index is omited, the entire
d[2,]                           # row or column is taken.
# Many functions perform all usual manipulations on matrices:
e <- t(d)                       # Transpose operator.
e
f <- d %*% e                    # %*% is the ordinary matrix product.
f
h <- cbind(f,f)                 # Binding columns and rows
j <- rbind(f,f)
h
j
invf <- solve(f)                # "solve" with only one argument,
                                # inverts a matrix.
                                # We can check that,
af <- invf %*% f
                                # is (except for rounding error)
                                # the unit matrix.
af
# Data need not be only numeric:
nombres <- c("Juan","Pedro" ,"Andres")
nombres
                                # We can create lists, joining dissimilar
                                # dissimilar types of variables.

ejemplo <- list(nombres,c(1,2,3))
ejemplo
# There is a fairly rich assortment of functions dealing with distributions. All common plus some not-so-common distributions are represented. The sintax is {r,d,p,q} + distribution name, meaning respectively "random number", "density" (or probability function, for discrete distributions), "distribution function" and "quantile function".

dbinom(x=2,size=10,prob=0.4)    # Rich set of library functions, in particular
                                #   to deal with common distributions.
choose(n=10,k=2)*0.4^2*0.6^8    # Let's check
choose(10,2) * 0.4^2 * 0.6^8    # No need to type argument names, if given in
                                # right order.
pbinom(2,10,0.4)                # p{binom,norm,...} is the distribution function.
dbinom(0,10,0.4) +              # Let's check (notice free format and way to
  dbinom(1,10,0.4) +            #   split over several lines).
  dbinom(2,10,0.4)
The last sum can be done even more easily. dbinom()  (and many other functions) is vectorized, so we can write:
dbinom(x=0:2,size=10,prob=0.4)
sum(dbinom(x=0:2,size=10,prob=0.4))
# The normal distribution is of course represented:
pnorm(1.96)                     # By default, N(0,1)...
pnorm(3.96,mean=2,sd=1)         # ...but can specify different mean and sd.
dnorm(0.0)                      # This is the density of a continuous distribution.
1/sqrt(2*pi)                    # Why the same value as the previous line?
# The quantile function is the inverse of the distribution function (when such an inverse does exist, which is not the case of course for discrete distributions):
qnorm(0.95)                     # Given the probability, we can also get the
qnorm(0.975)                  
qnorm(0.999)
qbinom(0.99,size=10,prob=0.4)   # Same with discrete distributions (approx.)
pbinom(9,10,0.4)                # Let's check.
pbinom(8,10,0.4)  
# There are many functions to graph output. This shows how to illustrate the fact that a binomial distribution can (for large enough np) be well approximated by a normal.
#
#   The N(m=1000*0.45, sigma=1000*0.45*.55) as an
#   approximation of the Binomial(p=0.45,n=1000)
#
a <- rbinom(n=1000000,prob=0.45,size=1000)
max(a)
hist(a,freq=TRUE)
hist(a,probability=TRUE)
hist(a,probability=TRUE,breaks=80,main="One million binomial observations")
curve(dnorm(x,mean=450,sd=sqrt(1000*.45*.55)),
      from=350,to=560,col="red",add=TRUE)
