R Language
R Language
1
2
3
4
Programming ‘R’ {
5
6
[For Beginners Workshop]
7
8 < Introduction to R programmng language >
9
10
11
12 }
13
14
plot
1
2 Table Of ‘Contents’ {
3
4 01 Getting to know R
5 < An overview on how R was born and
6 work>
7 02 Your first line of code
8 < Data structure, loop, input data,
9 debug>
10
11 03 Summary
12 < Summary and further reading>
13
14 }
plot
1
2
3 01 {
4
5
6
[Getting to know R]
7
8 < R is a programming language and environment specifically
9 designed for statistical computing and graphics
10 >
11
12
13 }
14
plot
1 Introduction{
2
###############################################################
3
4 # Most frequently used tools for both classic statistical
# and cutting-edge data analysis across many research
5 # disciplines
6
# Evolved into a multipurpose programming environment with # comprehensive spatial
7 data handling capabilities.
8
# Offer incredible number of functionalities through so-
9 # called packages
10
11 # Using command line interface
1
2
3
4 IDE{
5 ###############################################################
6
# Rstudio
7 # Pycharm
8
9 ##########################################################
10
11 }
12
13
14
Beginer.R plot
1
2
3
Installation{
4
5 ###############################################################
6 # R from https://www.r-project.org.
7 # An IDE that provides a better interface for working
8 # with R. We use Pycharm in the following sections.
9 ##########################################################
10
11
12
13 }
14
plot
1
2
3
4 02 {
5
6
7
[Your first line of code]
8
9
10 < Data structure, loop, input data, debug>
11
12
13
14
}
Beginer.R plot
1 x <- 4 + 10
2 plot(x)
3
4
5
6
7
8
9
10
11
12
13
14
Beginer.R plot
Classing_data.R plot
1 # vector
v <- c(1, 2, 6, 5, 9) #create a vector
2 v[3] # extracts the 3rd value of the vector ->6
3 v[3:5] # extracts the 3rd to 5th value -> 6 5 9
4
5 # matrix
m <- matrix(rnorm(10), ncol = 5, nrow = 2) #create a random matrix with 10
6 values
7 m[1, 2] # extracts the value which is located in the 1st row, 2nd column ->
8 -0.1563419
m[1, 1:2] # all values in the first row and 1st to 2nd column -> 0.2404937
9 -0.1563419
10
11 # data.frame
12 df[1, 2] # same as for the matrix, first row, second column position
df[1, 1:2] # same as for the matrix, first row, 1st to 2nd colum
13
14
Data_indexing .R plot
# data.frame
1 df[1, 2] # same as for the matrix, first row, second column position
2 df[1, 1:2] # same as for the matrix, first row, 1st to 2nd colum
3
# data.frame
4 df <- data.frame(colA = c(4, 6, 7, 3, 9), colB = c(1, 2, 5, 8, 10))#create a
5 data frame
6 df[1, 2] # same as for the matrix, first row, second column position -> 1
7 df[1, 1:2]# same as for the matrix, first row, 1st to 2nd column ->
# colA colB
8 #1 4 1
9
10 df$colA #subset data in column A ->4 6 7 3 9
11 plot(df$colA, df$colB) #plot data by column names
12
13
14
Importing_data.R plot
1 #importing data
df <- read.table("C:/Users/KevinLe/PycharmProjects/R_project/My_data.csv",
2 header = T)
3 df <- read.csv(" C:/Users/KevinLe/PycharmProjects/R_project/My_data.csv ")
4
5 #exporting data
write.table(df,file="C:/Users/KevinLe/PycharmProjects/R_project/My_data.csv ")
6 write.csv(df, file="C:/Users/KevinLe/PycharmProjects/R_project/My_data.csv ")
7
8 #set working directory
setwd("C:/Users/KevinLe/PycharmProjects/R_project/My_data.csv")
9
10
11
12
13
14
Exporting_data.R plot
1 #declare function
your_fuction <- function (x,y){
2
z <- x + y
3 return(z)
4 }
5
your_fuction(5,15)
6 #out put: 15
7
8
9
10
11
12
13
14
Loop.R plot
1 #loop
2 results <- list()
for (i in 1:length(my_list)) {
3 results[[i]] <- my_list[[i]] * 10
4 }
5
6
7
8
9
10
11
12
13
14
Script.R plot
1 #install package
2 install.packages("ggplots")
3
#load package
4 library("ggplot2")
5
6
7
8
9
10
11
12
13
14
Libraries.R plot
Debug.R plot
1
2 X <- raster(“ÿour_file.tif“)
3
4
5
6
7
8
9
10
11
12
13
14
Debug.R plot
Notion.R plot
1
2 myAggRaster <- raster::aggregate(myRaster, 2)
3
4
5
6
7
8
9
10
11
12
13
14
plot
1
2
3
4 03 {
5
6
7
[Summary]
8
9
10 < Summary and further reading>
11
12
13
14
}