Tutorial 1: Introdcution to R Programming

Mr. Zebin Yang
Sep. 11, 2018

  • I am a year 2 PhD Student at SaaS, studying machine learning.
  • Office Hour: Thursday 13pm-16pm
  • Location: RR114

Welcome to R!

  • R website: https://cran.r-project.org
  • R is a freely available programming language and is also the most widely used statistics programming language.
  • The core of R is an interpreted computer language which allows branching and looping as well as modular programming using functions.
  • R has the state-of-art graphics capability.

Intended Learning Outcomes

  • Learn R's basic data structure and syntax.
  • Capable to use R for simple data processing.

Practice

  • datacamp

https://campus.datacamp.com/courses/free-introduction-to-r/

“Hello, World!” program

  • R Command Prompt

It is easy to start your R command prompt by just typing commands at console. For example

myString <- "Hello, World!"
print(myString)
[1] "Hello, World!"

“Hello, World!” program

  • R Script File

Suppose we have a file “test.R” contatining the following codes.

myString <- “Hello, World!”
print(myString)

Then we can run this file in the command line by:

Rscript test.R

Data Frame

  • print a data.frame
head(df, 6)
  V1 V2
1  1  4
2  2  5
3  3  6
summary(df)
       V1            V2     
 Min.   :1.0   Min.   :4.0  
 1st Qu.:1.5   1st Qu.:4.5  
 Median :2.0   Median :5.0  
 Mean   :2.0   Mean   :5.0  
 3rd Qu.:2.5   3rd Qu.:5.5  
 Max.   :3.0   Max.   :6.0  

Data Frame

  • Adding columns
df$newx = c("IT","HR","Finance")
head(df)
  V1 V2    newx
1  1  4      IT
2  2  5      HR
3  3  6 Finance
  • Removing columns
df$newx = NULL
head(df)
  V1 V2
1  1  4
2  2  5
3  3  6

Thanks

  • Have a good day!