0% found this document useful (0 votes)
52 views24 pages

R Language

The document provides an introduction to the R programming language, covering topics such as getting started, data structures, importing and exporting data, functions, loops, and debugging. It includes code examples and discusses using R for statistical analysis and graphics.

Uploaded by

Lam Tùng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views24 pages

R Language

The document provides an introduction to the R programming language, covering topics such as getting started, data structures, importing and exporting data, functions, loops, and debugging. It includes code examples and discusses using R for statistical analysis and graphics.

Uploaded by

Lam Tùng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

plot

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

12 # Free under GNU license


13 ##########################################################
14 }
plot

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 v <- c(5, 6, 2, 7, 9) #declare a vector


2 v <- rnorm(50) plot(v)
3 m <- matrix(rnorm(15), ncol = 5, nrow = 3) #create a vector that contains 15
random normally distributed numbers
4 m
5            [,1]       [,2]       [,3]       [,4]        [,5]
6 [1,] 0.07635775  0.1477844 -0.1930893 -0.6800753 -0.05951253
7 [2,] 0.79320350 -2.1491485 -0.1415701  1.7710769 -0.66890892
[3,] 1.82232980  1.9396745  0.4598449 -0.4504710 -1.85964974
8
9 #create a data frame by converting a matrix to a data frame
10 df <- data.frame(m)
11 #create a data frame from scratch
12 df <- data.frame(colA = c(4, 6, 7, 3, 9), colB = c("a", "b","c", "d", "e"))
13
14 #declare a list with the variables created earlier
l <- list(our_vector = v, our_matrix = m, our_data_frame = df)
Data_indexing .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 write.table(df, file = "path/to/mynewtable.csv")


2 write.csv(df, file = "path/to/mynewtable.csv")
3
#set working directory
4 setwd("C:/path/to/your/working/directory")
5
6 my_list <- as.list(sample(1:100, 30))#create a list with 30 random
numbers between 1 and 100
7 #print(my_list) #print list
8
9
10
11
12
13
14
Function.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 # A short description of your script


# #############
2 # Purpose: This is a script to do analysis xy. Data sets will be ...
3 # Author: your name
4 # Date: month, year
5 # R version and packages: 4.1.1
# ##############
6 # Import format: csv, tab-delimited
7 # Output format: plot
8 # Import the csv with x and y coordinates
data_in <- read.csv("C:/Users/KevinLe/PycharmProjects/R_project/My_data.csv")
9
# check first entries
10 head (data_in)
11 # overview statistics
12 summary (data_in)
# plot data
13 plot (data_in)
14
Script.R plot
Libraries.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
}

You might also like