R Introduction Part 1
- This is my lecture note written for undergraduate R programming course, which is a supplementary course of the Advanced Macroeconomics course.
- Most figures and tables are extracted from R in Action.
- All exercises are copied from my teacher Jing Fang’s R Workshop, you can find the data for exercises on the website of R Workshop.
What is R ?
R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team, of which Chambers is a member. The R language is widely used among statisticians and data miners for developing statistical software and data analysis. For more information, please see Wiki: R(programming language)
Why choose R?
- R is a GNU project and totally free for everyone.
- R runs on a wide array of platforms, like Windows, Unix and Mac OS. You can even run R programming on your iPhone or iPad!
- R can easily import data from various types of data sources, including text files, database management systems, statistical packages, and specialized data repositories. It can write data out to these systems as well.
- R contains advanced statistical routines not yet available in other packages. In fact, new methods become available for download on a weekly basis.
- R has state-of-the-art graphics capabilities (try the package: ggplot2).
- R has a global community of more than 2 million users and developers who voluntarily contribute their time and technical expertise to maintain, support and extend the R language and its environment, tools and infrastructure.
Comparing R with other mainstream softwares
- Stata: Widely used in Economics field.
- SAS: excels at coping with big data, the syntax is awkward.
- Matlab: has strong ability of numerical value calculation and is incredible large.
- SPSS: widely used in Business Management.
- Eviews: is good at Time Series Analysis.
** Most important:** They are all commercial software, which means they are not free to individual user.
Installation of R
- How to get R?
R can be download from CRAN
RStudio is an amazing IDE for R. I strongly recommend you to run R program on RStudio.
RStudio can be download from RStuio Website
Packages of R
One feature of R is that most functions can be achieved by the installation of different packages. There are 6523 packages on the websit of CRAN and the number is still growing.
- Install packages
1.Install packages manually
Download package from This Page, RStudio->Tools->Install Packages
2.Install packages by using command
(1) Use command
install.packages(), then choose the package you need.
(2) Use command
install.packages("lmtest"), R will install the “lmtest” package and those supporting packages automatically.
- Load packages by using command
library(lmtest)to load the “lmtest” package before you want to use this package.
Note: You only need to install packages once, but you need to reload those needed packages every time after you restart RStudio.
- Update packages
update.packages()to update those packages you have installed.
- Load packages by using command
Other notes on using R
- R is a case sensitive language.
<-can both be used as assignment symbol.
Personally, I do not think there is a big difference between them and I highly recommend using
<-. For more information on the subtle difference between these two symbol, please see stackoverflow and 知乎.
- We use
#in R to indicate the following part is comment and should not be compiled.
- R cannot read
setwd("c:\myprogram"), you should use ‘setwd(“c:/myprogram”)’ or
- Please wisely use
?the_name_of_funcitonto read help documentation. The website Rseeker and Google are also really helpful.
A dataset is usually a rectangular array of data with rows representing observations
and columns representing variables. The following table provides an example of a hypothetical
How to construct a dataset?
1.Import or type data into the data structures.
2.Choose a type of data structures to store data.
From the following figure, we can see that R can cope with different data formats.
read.table()#This can be used to read txt file, the argument
na.strings="."should be used to convert the missing value (“.” in txt file) to NA value.
read.csv()#This can be used to read CSV file, highly recommended.
load()#This can be used to load Rdata file.
Note: You can find out more information on how to input other different types of data in the book R in Action (R语言实战).
R has a wide variety of objects for holding data, including scalars, vectors, matrices,
arrays, data frames, and lists. They differ in terms of the type of data they can hold,
how they are created, their structural complexity, and the notation used to identify and
access individual elements. The figure shows a diagram of these data structures.
Vectors are one-dimensional arrays that can hold numeric data, character data, or logical
data. The combine function
c() is used to form the vector.
a=c(1,2,3,4) #This is a numeric vector. b=c("one","two","three") #This is a character vector. c=c(TRUE,FALSE,TRUE) #This is a logical vector.
A matrix is a two-dimensional array where each element has the same mode (numeric,
character, or logical). Matrices are created with the
x=matrix(1:20, nrow=5, ncol=4) #This can create a 5*4 matrix using the number 1~20.
The comment operation symbols in matrix:
Dimensions of matrix x:
Rows of matrix x:
Columns of matrix x:
Transpose of matrix x:
The value of the determinant of matrix x:
If the determinant is not 0, then we can get the inverse of matrix x:
Eigenvalue and eigenvector:
y$val is eigenvalue,
y$vec is eigenvector.
Multiplication of matrices:
a %*% b
Arithmetic operations and power operation on every element of matrix:
+ - * / ^
Personally, I think data frame is the most important and common data structure you will deal with in R. A data frame is more general than a matrix in that different columns can contain different modes of data (numeric, character, etc.).
patientID= c(1, 2, 3, 4) age = c(25, 34, 28, 52) diabetes = c("Type1", "Type2", "Type1", "Type1") status = c("Poor", "Improved", "Excellent", "Poor") patientdata= data.frame(patientID, age, diabetes, status) patientdata #patientdata is a data frame.
The different ways to read data from data frame:
patientdata[1:2] patientdata[c("diabetes", "status")] patientdata$age
Useful mathematical functions and statistical functions
Useful data operation functions
summary is a generic function used to produce result summaries of the results of various model fitting functions.
sum returns the sum of all the values present in its arguments.
3.nrow and ncol
ncol return the number of rows or columns present in x.
4.cbind and rbind
Take a sequence of vector, matrix or data-frame arguments and combine by columns or rows, respectively.
replace replaces the values in x with indices given in list by those given in values.
In this example, if the data in column V1 is less than 5, then those data will be converted to 0. This function is really useful when you want to convert your numerical variable to dummy variable.
Return subsets of vectors, matrices or data frames which meet conditions.
subset(data, subset, select)
Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form.
aggregate(data, by=list(data$variable), mean)
In this example, based on the column variable, R splits the data into subsets and computes mean of those subsets.