- How to install and use R
- R basics
- R coding style
- Some nice R tips
School of Economics and Management
Beihang University
http://yanfei.site
install.packages(c("forecast", "sos", "formatR"))
install.packages('forecast')
to install an package called ‘forecast’.library(forecast)
before using the package.## simple maths 1 + 2 + 3 1 + 2 * 3 ## assign a value to a variable x <- 1 y <- 2 z <- c(x,y) z ## function examples exp(1) cos(3.141593) log2(1)
## vectors c(0, 1, 1, 2, 3, 5, 8) 1:10 seq(1, 9, 2) rep(1, 10) length(rep(1, 10)) ## character vectors c("Hello world", "Hello R interpreter") ## vector calculation c(1, 2, 3, 4) + c(10, 20, 30, 40) c(1, 2, 3, 4) + 1 ## you can refer to elements by location in a vector b <- c(1,2,3,4,5,6,7,8,9,10,11,12) length(b) b b[7] b[1:6] b[c(1,6,11)] b > 5 b[b > 5]
matrix()
dim()
t()
rbind()
, cbind()
## create a matrix m <- matrix(c(1:6), 2, 3) n <- matrix(c(8:13), 2, 3) dim(m) t(m) m[1, 2] m[1, ] cbind(m, n) rbind(m, n)
list()
[[]]
or $
l <- list(a = c(1, 2), b = 'apple')
data.frame()
: tightly coupled collections of variables which share many of the properties of matrices and of lists, used as the fundamental data structure by most of R’s modeling software.L3 <- LETTERS[1:3] fac <- sample(L3, 10, replace = TRUE) d <- data.frame(x = 1, y = 1:10, fac = fac)
f <- function(x, y) { z <- c(x + 1, y + 1) return(z) } f(1, 2)
source()
if (condition){ do something } else { do something }
x <- 0 if (x > 1) { print('x is larger than 1') } else { print('x is not larger than 1') }
x <- 1:10 for(i in x) { print(i^2) }
MySummary()
where the input argument is x can be any vector and the output is a list that contains the basic summary (mean, variance, length, max and minimum values) of the vector you have supplied to the function.predict_ad_revenue.R
foo.R
mean
, median
etc.x <- c(1:10) x.average<-mean(x,na.rm=TRUE)
\(\Rightarrow\)
x.average <- mean(x, na.rm = TRUE)
Don’t be afraid of splitting one long line into individual pieces!
n <- matrix(sample(1:100, 9), nrow = 3, ncol = 3, byrow = TRUE)
\(\Rightarrow\)
n <- matrix(sample(1:100, 9), nrow = 3, ncol = 3, byrow = TRUE)
if (y < 0) {print("y is negative")}
\(\Rightarrow\)
if (y < 0) { print("y is negative") }
if (y < 0) { print("y is negative") }
\(\Rightarrow\)
if (y < 0) { print("y is negative") }
.R
files tidy using tidy_dir()
.x <- c(1:10) x.mean = mean(x) x.var = var(x)
\(\Rightarrow\)
## ============================================= ## Title ## Author: Yanfei Kang ## Date: Mar 23, 2017 ## Description: your purpose ## ============================================= x <- c(1:10) ## getting the mean of x x.mean = mean(x) ## getting the variance of x x.var = var(x)
CalculateSampleCovariance <- function(x, y, verbose = TRUE) { ## Computes the sample covariance between two vectors. # ## Args: ## x: One of two vectors whose sample covariance is to be calculated. ## y: The other vector. x and y must have the same length, greater than one, ## with no missing values. ## verbose: If TRUE, prints sample covariance; if not, not. Default is TRUE. # ## Returns: ## The sample covariance between x and y. n <- length(x) ## Error handling if (n <= 1 || n != length(y)) { stop("Arguments x and y have different lengths: ", length(x), " and ", length(y), ".") } if (TRUE %in% is.na(x) || TRUE %in% is.na(y)) { stop(" Arguments x and y must not have missing values.") } covariance <- var(x, y) if (verbose) cat("Covariance = ", round(covariance, 4), ".\n", sep = "") return(covariance) }
library(forecast) help.search("auto.arima") ??auto.arima
library(sos) findFn("arima") RSiteSearch("arima")
?sort
for the usage of the function sort()
.forecast:::estmodel
for hidden functions.main.R
or main.Rmd
.functions.R
contains all non-packaged functions used in the project.Write a function to solve the roots of given quadratic equation \(ax^2 + bx + c = 0\) with \(a\), \(b\) and \(c\) as input arguments.
Test your function on some simple equations.
Keep in mind the styles we have learnt.
.R
files and put them in a folder.tidy_dir()
to make them tidy.