You are on page 1of 13

PRAKTIKUM KE-3

KLIMATOLOGI TROPIKA (GFM321)


INTRODUCTION TO R LANGUAGE
PROGRAMMING
RK. Pasca 2 GFM
Software
Project analysis slide 3

https://cran.r- https://download1.rstudio.or
project.org/bin/windows g/desktop/windows/RStudio
/base/R-3.6.1-win.exe -1.2.1335.exe
Outline

1. Intro to Basic
2. Create a Vector
3. Create a Matrix
4. Create a Data Frame
5. Create an Array
6. Plot the data using ggplot2
NOW, OPEN YOUR R STUDIO
APPLICATION!
1. Intro to Basic
Project analysis slide 3
• Arithmetic with R
• R can be used as a simple calculator: subtraction (-), addition (+),
multiplication (*), division (/), exponentiation (^), modulo (%%)
• Expression of variable
Ex: my_var = “Hello World” or my_var <- “Hello World”
number_1 = 6
number_2 = 4
addition_num = number_1 + number_2
• How to print variable? Just type the variable into the console or use
command print()
• Basic data types in R: numeric, integers, logical, character
• How to check? Use command function class()
2. Create a Vector
Project analysis slide 3
• You can create a vector with the combine function c()
Ex: numeric_vector = c(3,4,5)
string_vector = c(“a”,”b”,”c”)
boolean_vector = c(TRUE, FALSE)
• You can create a vector name with function names()
Ex: names(numeric_vector) = string_vector
3. Create a Matrix
Project analysis slide 3
• Create a matrix with function matrix(data = NA, nrow = 1, ncol = 1,
byrow = FALSE, dimmnames = NULL)
Example:
mdat <- matrix(data = c(1,2,3,4), nrow = 2, ncol = 2, byrow = TRUE,
dimnames = list(c(“row1”,”row2”),c(“col1”,”col2”))
• You can create a matrix name with function rownames() and
colnames()
• Matrix usually can be used for looping calculations
4. Create a Data Frames

Project analysis slide 3


• Create a matrix with function data.frame(…)
Example:
df <- data.frame(col.A = c(1,3,4,5), col.B = c(“a”,”b”,”c”,”d”))
• col.Aand col.B are the names of data frames in df variable
• You can add row names with add row.names argument into the
data.frame()
Example:
new.df <- data.frame(col.A = c(1,3,4,5), col.B = c(“a”,”b”,”c”,”d”),
row.names = c(“row.1”,”row.2”,”row.3”,”row.4”))
5. Create an Array
Project analysis slide 3
• Create an array with function array(data = NULL, dim = length(data))
Example:
new.array <- array(data = c(1,3,4,5,11,12,13,14), dim = c(2,2,2))
Summary
Project analysis slide 3
Vector Matrix Data Frames Array
• 1 Dimension • 2 Dimensions • 2 Dimensions • ≥1
• Used for Dimension
ggplot • Used for
NetCDF
format

list(...)
6. Data Plot using ggplot2

Project analysis
1. Install packages 
slide 3
install.packages(“tidyverse”)
install.packages(“readxl”) #package to import excel file

2. Import your excel file by using readxl package


library(readxl)
data.excel = read_xlsx(path = “D:/example.xlsx”, sheet = Sheet1)
View(data.excel) #To look your data

3. Let’s start to plot (Tips: The data must be form as data frames)
library(ggplot2)
plot.df = ggplot(data = data.excel, aes(x = wind, y= direction)) + geom_line() #line
graphs
plot.df1 = ggplot(data = data.excel, aes(x = time, y= temperature)) + geom_histogram() #bar
graphs
6. Data Plot using ggplot

Project analysis
1. Install packages 
slide 3
install.packages(“tidyverse”)
install.packages(“readxl”) #package to import excel file

2. Import your excel file by using readxl package


library(readxl)
data.excel = read_xlsx(path = “D:/example.xlsx”, sheet = Sheet1)
View(data.excel) #To look your data

3. Let’s start to plot (Tips: The data must be form as data frames)
library(ggplot2)
plot.df = ggplot(data = data.excel, aes(x = wind, y= direction)) + geom_line() #line
graphs
plot.df1 = ggplot(data = data.excel, aes(x = time, y= temperature)) + geom_histogram() #bar
graphs
Thank You

You might also like