Scientific Calculator
Class
Vectors
Matrices
Arrays
?Arithmetic or help(“Arithmetic”)
example(integer) , demo(graphics)
2+3
## [1] 5
14/6
## [1] 2.333333
14/6+5
## [1] 7.333333
14/(6+5)
## [1] 1.272727
3^2
## [1] 9
2^3
## [1] 8
sqrt(x=9)
## [1] 3
sqrt(x=5.311)
## [1] 2.304561
f <- expression(x^2+3*x) # you can check ?expression
D(f,'x') # Calculate (first) derivative of f with respect to x
## 2 * x + 3
Variables are defined with different data types
Also
Variables are assigned with R-Objects
—> The data type of the R-object
Any number with (or without) a decimal point.
a <- 3.8
a
## [1] 3.8
class(a)
## [1] "numeric"
b <- 4
b
## [1] 4
class(b)
## [1] "numeric"
c <- sqrt(2)
c
## [1] 1.414214
class(c)
## [1] "numeric"
d <- 3.5:9.5
d
## [1] 3.5 4.5 5.5 6.5 7.5 8.5 9.5
class(d)
## [1] "numeric"
class(1)
## [1] "numeric"
Kind of a sub-class of the numeric class.
The suffix L tells R to store this as an integer.
a <- 7
a
## [1] 7
class(a)
## [1] "numeric"
b <- 7L
b
## [1] 7
class(b)
## [1] "integer"
c <- 5:9
c
## [1] 5 6 7 8 9
class(c)
## [1] "integer"
d <- 5.1:9.1
d
## [1] 5.1 6.1 7.1 8.1 9.1
class(d)
## [1] "numeric"
class(3.2L)
## [1] "numeric"
pi
sqrt(2)^2-2
Numeric (64-bit) -> big memery and calculations
Integer (32-bit) -> Constant lalues like ID
6 digits after decimal
16 significant digits
Complex: x2 = −1 (imaginary number)
a <- i # This will give error
b <- 1i
b
## [1] 0+1i
class(b)
## [1] "complex"
class(1+2i)
class(2iL)
try
class(((1i^2)^2))
## [1] "complex"
is.complex((1i^2)^2)
## [1] TRUE
isTRUE(is.complex((1i^2)^2))
## [1] TRUE
(1i^2)^2
## [1] 1+0i
TRUE or FALSE - Logical Operators
5 < 9
## [1] TRUE
5 < -9
## [1] FALSE
a <- 5 < -9
class(a)
## [1] "logical"
1:10
## [1] 1 2 3 4 5 6 7 8 9 10
1:10 >= 5
## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
x <- 1:10 >= 5
1:10 < 2
## [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
y <- 1:10 < 2
x | y
## [1] TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
z <- x | y
z
## [1] TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
!z
## [1] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
class(z)
## [1] "logical"
b <- 4:8
b
## [1] 4 5 6 7 8
c <- 7:11
c
## [1] 7 8 9 10 11
b != c
## [1] TRUE TRUE TRUE TRUE TRUE
d <- 5:12
b != d
## Warning in b != d: longer object length is not a multiple of shorter object
## length
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Data type consists of letters or words. String.
single quotes: ’ … ’ or double quotes " … "
name <- emir # This will give an error
name <- 'emir'
name
## [1] "emir"
class(name)
## [1] "character"
a <- 23
a
## [1] 23
class(a)
## [1] "numeric"
b <- '23'
b
## [1] "23"
class(b)
## [1] "character"
print('hello')
## [1] "hello"
cat('hello')
## hello
class(print('hello'))
## [1] "hello"
## [1] "character"
class(cat("hello"))
## hello
## [1] "NULL"
Null, Infinity, Not a Number, Not Available
NULL # Null (“empty” entity)
## NULL
Inf # Infinity
## [1] Inf
class(Inf)
## [1] "numeric"
Inf*-9
## [1] -Inf
is.finite(Inf)
## [1] FALSE
1/0
## [1] Inf
NaN # Not a Number
## [1] NaN
class(NaN)
## [1] "numeric"
-Inf+Inf
## [1] NaN
is.nan(5^(-Inf/Inf))
## [1] TRUE
0/0
## [1] NaN
NA # Not Available (“missing” entity)
## [1] NA
class(NA)
## [1] "logical"
Sys.Date( )
## [1] "2020-10-22"
date()
## [1] "Thu Oct 22 01:31:51 2020"
date <- "2007-06-22"
class(date)
## [1] "character"
date1 <- as.Date(date) # Coercion
class(b)
## [1] "character"
date2 <- as.Date("2004-02-13")
date1 - date2
## Time difference of 1225 days
date_difference <- date1 - date2
class(date_difference)
## [1] "difftime"
%d day as a number (0-31) 01-31
%a abbreviated weekday Mon
%A unabbreviated weekday Monday
%m month (00-12) 00-12
%b abbreviated month Jan
%B unabbreviated month January
%y 2-digit year 07
%Y 4-digit year 2007
today <- Sys.Date()
format(today, format="%B %d %Y")
## [1] "October 22 2020"
3
## [1] 3
class(3)
## [1] "numeric"
as.numeric(3)
## [1] 3
as.character(3)
## [1] "3"
as.logical(3)
## [1] TRUE
FALSE
## [1] FALSE
class(FALSE)
## [1] "logical"
as.character(FALSE)
## [1] "FALSE"
as.numeric(FALSE)
## [1] 0
as.numeric(TRUE)
## [1] 1
TRUE+TRUE
## [1] 2
class(TRUE+TRUE)
## [1] "integer"
The simplest data structure in R
Vectors are a list-like structure that contain items of the same data type.
spring_month <- "April"
spring_month
## [1] "April"
spring_months <- c("March", "April","May","June")
spring_months
## [1] "March" "April" "May" "June"
class(spring_months)
## [1] "character"
c means “combine”
myvec <- c(1, 3, 1, 42)
a <- 35
myvec2 <- c(3L, 3.45, 1e+03, 64^0.5, 2+(3-1.1)/9.44, a)
myvec3 <- c(myvec, myvec2)
myvec3
## [1] 1.000000 3.000000 1.000000 42.000000 3.000000
## [6] 3.450000 1000.000000 8.000000 2.201271 35.000000
x <- c("all", "b", "olive")
Length of a vector, length(vector_name)
length(x)
## [1] 3
Indexing element, vector_name[element_position]
x[2]
## [1] "b"
Manipulating element of vector, assigning arrow
x[2] <- "b_new"
x
## [1] "all" "b_new" "olive"
Note: In R, counting elements start position 1, not 0.
y <- c( 1.2, 5, "Rt", "2000", 20, 4905)
y [0]
## character(0)
class(y)
## [1] "character"
y
## [1] "1.2" "5" "Rt" "2000" "20" "4905"
Sequences
7:16.4
## [1] 7 8 9 10 11 12 13 14 15 16
a <- 7:16
a
## [1] 7 8 9 10 11 12 13 14 15 16
seq(from=7,to=16,by=3)
## [1] 7 10 13 16
seq(50,150,25)
## [1] 50 75 100 125 150
seq(50,149,25)
## [1] 50 75 100 125
seq(from=3,to=27,length.out=40)
## [1] 3.000000 3.615385 4.230769 4.846154 5.461538 6.076923 6.692308
## [8] 7.307692 7.923077 8.538462 9.153846 9.769231 10.384615 11.000000
## [15] 11.615385 12.230769 12.846154 13.461538 14.076923 14.692308 15.307692
## [22] 15.923077 16.538462 17.153846 17.769231 18.384615 19.000000 19.615385
## [29] 20.230769 20.846154 21.461538 22.076923 22.692308 23.307692 23.923077
## [36] 24.538462 25.153846 25.769231 26.384615 27.000000
Round
3/2
## [1] 1.5
round(3/2)
## [1] 2
round(5.1)
## [1] 5
round(pi)
## [1] 3
Repetition
rep(x=1, times=4)
## [1] 1 1 1 1
rep(x=c(3, 62, 8),times=3)
## [1] 3 62 8 3 62 8 3 62 8
rep(x=c(3, 62, 8),times=3,each=2)
## [1] 3 3 62 62 8 8 3 3 62 62 8 8 3 3 62 62 8 8
Sorting
sort(x=c(2.5, -1, -10, 3.44)) # decreasing=FALSE (default)
## [1] -10.00 -1.00 2.50 3.44
sort(x=c(2.5, -1,- 10, 3.44), decreasing=TRUE)
## [1] 3.44 2.50 -1.00 -10.00
Random - Uniform Distribution
runif(15, min = 20, max = 45)
## [1] 37.33403 43.27645 43.85245 40.93802 26.44975 23.31924 21.45879
## [8] 23.21350 39.94376 44.62040 36.69073 43.46085 35.03261 41.99214
## [15] 36.66242
runif(15, 20, 45)
## [1] 26.63005 42.79384 41.57672 31.05458 41.36726 41.98817 29.52497
## [8] 37.82627 34.39444 32.72802 38.40542 24.09379 43.23531 34.73173
## [15] 20.85018
runif(25, 60, 50)
## Warning in runif(25, 60, 50): NAs produced
## [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
## [18] NaN NaN NaN NaN NaN NaN NaN NaN
Random variable can be saved
set.seed(1)
runif(15, 20, 45)
## [1] 26.63772 29.30310 34.32133 42.70519 25.04205 42.45974 43.61688
## [8] 36.51994 35.72785 21.54466 25.14936 24.41392 37.17557 29.60259
## [15] 39.24604
Vectors indexed using two indices instead of one.
n <- runif(9,1,100)
n
## [1] 50.27222 72.04423 99.19870 38.62348 77.96708 93.53582 22.00211 65.51570
## [9] 13.42995
matrix(n, nrow = 3, ncol = 3)
## [,1] [,2] [,3]
## [1,] 50.27222 38.62348 22.00211
## [2,] 72.04423 77.96708 65.51570
## [3,] 99.19870 93.53582 13.42995
n2 <- runif(10,1,100)
matrix(n2, nrow = 3, ncol = 3)
## Warning in matrix(n2, nrow = 3, ncol = 3): data length [10] is not a sub-
## multiple or multiple of the number of rows [3]
## [,1] [,2] [,3]
## [1,] 27.454846 38.85641 48.72593
## [2,] 39.225295 87.09939 60.35702
## [3,] 2.325643 34.69455 49.86059
x <- as.numeric(seq(10,120,10))
mx <- matrix(x,3,4) # n, nrow, ncol
mx
## [,1] [,2] [,3] [,4]
## [1,] 10 40 70 100
## [2,] 20 50 80 110
## [3,] 30 60 90 120
class(mx)
## [1] "matrix"
class(mx[1])
## [1] "numeric"
typeof(mx)
## [1] "double"
mx[1,]
## [1] 10 40 70 100
mx[,2]
## [1] 40 50 60
mx[,2:4]
## [,1] [,2] [,3]
## [1,] 40 70 100
## [2,] 50 80 110
## [3,] 60 90 120
mx_new <- mx[,2:4]
mx
## [,1] [,2] [,3] [,4]
## [1,] 10 40 70 100
## [2,] 20 50 80 110
## [3,] 30 60 90 120
mx[2,3] <- "rose"
mx
## [,1] [,2] [,3] [,4]
## [1,] "10" "40" "70" "100"
## [2,] "20" "50" "rose" "110"
## [3,] "30" "60" "90" "120"
class(mx)
## [1] "matrix"
typeof(mx)
## [1] "character"
mx_new <- as.numeric(mx)
## Warning: NAs introduced by coercion
mx_new
## [1] 10 20 30 40 50 60 70 NA 90 100 110 120
class(mx_new)
## [1] "numeric"
typeof(mx_new)
## [1] "double"
m_mycol <- matrix(c(1, 2, 3, 4, 5, 6),
nrow = 2,
ncol = 3,
byrow = FALSE) # Default
m_mycol
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
m_byrow <- matrix(c(1, 2, 3, 4, 5, 6),
nrow = 2,
ncol = 3,
byrow = TRUE)
m_byrow
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
t(m_byrow)
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
length(mx)
## [1] 12
dim(mx)
## [1] 3 4
x <- 1:24
x
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24
array(x, dim = c(4,3,2)) # raw, col, level
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
arr <- array(x, c(4,3,2))
class(arr)
## [1] "array"
typeof(arr)
## [1] "integer"
arr <- array(data=10:33,dim=c(3,4,2))
arr
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 10 13 16 19
## [2,] 11 14 17 20
## [3,] 12 15 18 21
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 22 25 28 31
## [2,] 23 26 29 32
## [3,] 24 27 30 33
arr[2,2,1]
## [1] 14
arr[-1,,]
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 11 14 17 20
## [2,] 12 15 18 21
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 23 26 29 32
## [2,] 24 27 30 33
Scientific Calculator
Scientific Calculator
Problem: Compute double, triple or higher order integrals
install.packages("cubature")
library(cubature)
f <- function(x) 1
adaptIntegrate(f,lowerLimit = c(0,0,0),upperLimit = c(4,4,4))
$integral
[1] XX
Create a Function
Problem: Take a sample belonged to population and sum
pop <- 1:6 # This is my population
samp <- sample(pop, size = 2) # This is my sample, I choose two var.
sum(samp)
## [1] 9
pop
## [1] 1 2 3 4 5 6
samp
## [1] 5 4
I want to create a new function named roll()
roll <- function() {
pop <- 1:6
samp <- sample(pop, size = 2)
sum(samp)
}
roll()
## [1] 6
Create a Function
Problem: What if we removed one line of code from our function and changed the name pop to box ?
roll2 <- function() {
samp <- sample(box, size = 2)
sum(samp)
}
roll2() # This will give error
Re-create function
roll2 <- function(box) {
samp <- sample(box, size = 2)
sum(samp)
}
roll2(box = 1:4)
## [1] 5
roll2(box = 1:6)
## [1] 9
roll2(1:20)
## [1] 27
Create a Function
print()
*
a <- 2.3
(6*a+42)/(3^(4.2-3.62))
## [1] 29.50556
isTRUE((6*a+42)/(3^(4.2-3.62))==29.50556)
## [1] FALSE
Problem: Compute double, triple or higher order integrals
Problem: Compute double, triple or higher order integrals
Problem: Compute double, triple or higher order integrals
# install.packages("cubature")
library(cubature)
cube_f <- function(x) 1
adaptIntegrate(cube_f,lowerLimit = c(0,0,0),upperLimit = c(4,4,4))
## $integral
## [1] 64
##
## $error
## [1] 7.105427e-15
##
## $functionEvaluations
## [1] 33
##
## $returnCode
## [1] 0
# Any number with (or without) a decimal point.
a <- 3
# Sub-class of the numeric class. The suffix L tells R to store.
a <- 3L
# TRUE or FALSE - Logical Operators. < , > , == , >= , <= , != ...
a <- 3<2
# Data type consists of letters or words. String. with quotes: " … "
a <- "3"
is.XXX() and class()
name1 <- emir
name1 <- "emir"
name2 <- name1
name3 <- "name1"
number1 <- 32
number2 <- "32"
number3 <- 1:10
number4 <- seq(1,10)
var1 <- TRUE
var2 <- "TRUE"
answer1 <- is.logical(var1)
answer2 <- var1 + answer1 / 3
surname1 <- "toker"
print(name1)
print(surname1)
print(name1,surname1)
is.XXX() and class()
name <- "emir"
surname <- "toker"
print(c(name,surname)) # c means “combine”
Vector : The simplest data structure in R
name <- "emir"
surname <- "toker"
name_surname <- c(name,surname)
length(name_surname)
print(c("21","21"))
print(c("21",21))
print(c(21,21))
spring_months <- c("March", "April","May","June")
spring_months
length(spring_months)
dim(spring_months)
spring_months[1]
spring_months[3:4]
str(spring_months) # Structure
substr(spring_months, start = 1, stop = 3) # Substrings
strsplit(spring_months,"")
gsub("a", "A", spring_months) # Matching and Replacement
?str , ?substr , ?strsplit , ?gsub
Vectors indexed using two indices instead of one.
[ row, col ]
a <- c(1:3)
# str(a) and dim(a) and length(a)
b <- matrix(1:3, nrow = 1, ncol = 3)
# str(b) and dim(b) and length(b)
a <- c(1:3)
b <- matrix(1:3, nrow = 1, ncol = 3)
a <- c(1:3)
b <- matrix(1:3, nrow = 1, ncol = 3)
a
## [1] 1 2 3
b
## [,1] [,2] [,3]
## [1,] 1 2 3
c <- matrix(1:9, nrow = 3, ncol = 3)
c <- matrix(1:9, nrow = 3, ncol = 3)
c
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
d <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)
d <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)
d
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
my_mat <- matrix(runif(n=20, min=0, max=100), nrow = 4, ncol = 5)
my_mat <- matrix(runif(n=20, min=0, max=100), nrow = 4, ncol = 5)
my_mat
## [,1] [,2] [,3] [,4] [,5]
## [1,] 65.08705 8.424691 34.66835 86.43395 43.46595
## [2,] 25.80168 87.532133 33.37749 38.99895 71.25147
## [3,] 47.85452 33.907294 47.63512 77.73207 39.99944
## [4,] 76.63107 83.944035 89.21983 96.06180 32.53522
add <- matrix(seq(from=10, to=60, by=10), nrow = 2, ncol = 3)
add <- matrix(seq(from=10, to=60, by=10), nrow = 2, ncol = 3)
add
## [,1] [,2] [,3]
## [1,] 10 30 50
## [2,] 20 40 60
my_mat[2:3,2:4] <- add
my_mat[2:3,2:4] <- add
my_mat
## [,1] [,2] [,3] [,4] [,5]
## [1,] 65.08705 8.424691 34.66835 86.43395 43.46595
## [2,] 25.80168 10.000000 30.00000 50.00000 71.25147
## [3,] 47.85452 20.000000 40.00000 60.00000 39.99944
## [4,] 76.63107 83.944035 89.21983 96.06180 32.53522
arr <- array(1:24, dim = c(4,3,2)) #raw,col,level
str(arr)
## int [1:4, 1:3, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
# dim(arr)
# length(arr)
x <- 1:24
x <- 1:24
x
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24
arr <- array(x, dim = c(4,3,2)) #raw,col,level
arr <- array(x, dim = c(4,3,2)) # raw, col, level
arr
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
[ row, col, level ]
arr <- array(data=10:30,dim=c(2,5,2))
arr
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 10 12 14 16 18
## [2,] 11 13 15 17 19
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 20 22 24 26 28
## [2,] 21 23 25 27 29
arr[2,2:4,1:2]
## [,1] [,2]
## [1,] 13 23
## [2,] 15 25
## [3,] 17 27
arr[1,1:5,2]
## [1] 20 22 24 26 28
array <- array(data=seq(2,144,2),dim=c(3,6,4))
array
## , , 1
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 2 8 14 20 26 32
## [2,] 4 10 16 22 28 34
## [3,] 6 12 18 24 30 36
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 38 44 50 56 62 68
## [2,] 40 46 52 58 64 70
## [3,] 42 48 54 60 66 72
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 74 80 86 92 98 104
## [2,] 76 82 88 94 100 106
## [3,] 78 84 90 96 102 108
##
## , , 4
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 110 116 122 128 134 140
## [2,] 112 118 124 130 136 142
## [3,] 114 120 126 132 138 144
array[1,1,1:4]
array[1,1,]
array[1,1,4:1]
array[1,1,c(1:4!=2)]
array[1,1,2]
array[1,1,which(x==2)]
array[1,1,which(x<=2)]
array[1,1,2:4]
array[1,1,-1]
array[1,1,c(-1,-2)]
array[1,2:5,2]
array[1,c(2,3,4,5),2]
array[1,c(2,5),2]
array[1,c(2,5),2:3]
array[,c(2,5),2:3]
[ row, col, level ]
Factors are a special variable type for storing categorical variables.
They sometimes behave like strings, and sometimes like integers.
gender = c(male", "female", "male", "male", "female")
gender
class(gender)
str(gender)
gender[2]
gender_factor <- factor(c("male", "female", "male", "male", "female"))
gender_factor
class(gender_factor)
str(gender_factor)
gender_factor[2]
person=c("Peter","Lois","Meg","Chris","Stewie")
age=c(42,40,17,14,1)
sex=factor(c("M","F","F","M","M"))
married=c(TRUE,TRUE,FALSE,FALSE,FALSE)
den <- matrix(c(person,married),5,2)
den <- matrix(c(age,married),5,2)
den <- matrix(c(person,age),5,2)
den <- matrix(c(person,sex),5,2)
no need to Combine
df <- data.frame(person,married)
df
class(df)
dim(df)
length(df)
str(df)
df$
df$person
as.character(df$person)
df <- data.frame(person,age,sex,married,stringsAsFactors=FALSE)
df
str(df)
person="Brian"
age=7
sex=factor("M")
married=FALSE
new_record_row <- data.frame(person,age,sex,married)
new_df <- rbind(df,newrecord) # Combine R Objects by Rows
surname=c("Yilmaz","Zeki","Sahin","Caliskan","Uslu","Guzel")
new_record_col <- data.frame(surname, stringsAsFactors=FALSE)
new_df2 <- cbind(new_df, new_record_col) # Combine R Objects by Columns
new_df2
new_df2[c(5,6),]
new_df2[c(5,6),] <- new_df2[c(6,5),]
new_df2[5]
a <- 9:14
a
a[2]
a[2,]
b <- matrix(a,2,3)
b
b[2]
b[2,]
new_df2
new_df2[1]
length(new_df2[1])
dim(new_df2[1])
new_df2[[1]]
length(new_df2[[1]])
dim(new_df2[[1]])
new_df2[[1]][2]
new_df2[[1]][2:5]
new_df2$person[2]
new_df2$person[2:5]
new_df2
new_df2[2:3,1:5]
new_df2[2:3,]
new_df2[2,1]
new_df2[2:2,1:1]
attributes(new_df2)
matrix <- matrix(data=1:4,nrow=2,ncol=2)
vector <- c(T,F,T,T)
var <- "hello"
data_frame <- new_df2
list <- list(matrix,vector,var,data_frame)
class(list)
str(list)
dim(list)
length(list)
Read
Write
Plot
library(help="datasets")
list.files("/Users/emirtoker/Desktop/Dersler/Memurluk/Software_Tools_for_Earth_&_Environmental_Science/Software_Tools_R_Github/Presentation")
file.choose()
read.table(file = "18397_Cekmekoy_Omerli_15dk.txt")
read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
header=TRUE, sep=";")
read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
header=TRUE, sep=";", na.strings="-9999")
mydata_txt <- read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
header=TRUE,
sep=";",
na.strings="-9999")
str(mydata_txt)
mydata_csv <- read.csv(file="18397_Cekmekoy_Omerli.csv",
header=TRUE,
na.strings="-9999")
str(mydata_csv)
url <- "https://web.itu.edu.tr/tokerem/18397_Cekmekoy_Omerli_15dk.txt"
urldata_txt <- read.table(url,
header=TRUE,
sep=";",
na.strings="-9999")
Write .TXT and .CSV
write.table(x=urldata_txt,file="somenewfile.txt")
write.table(x=urldata_txt,file="somenewfile.txt",
sep=";",na="-9999",quote=FALSE,row.names=FALSE)
new_df2
write.table(x=new_df2,file="dffile.txt",
sep=";",na="-9999",quote=FALSE,row.names=FALSE)
write.table(x=new_df2,file="dffile.csv",
sep=";",na="-9999",quote=FALSE,row.names=FALSE)
foo <- c(1.1,2,3.5,3.9,4.2)
bar <- c(2,2.2,-1.3,0,0.2)
plot(foo,bar)
plot(foo,bar)
plot(foo,bar,type="l")
plot(foo,bar,type="b",main="My lovely plot",xlab="x axis label", ylab="location y")
plot(foo,bar,type="b",main="My lovely plot",xlab="",ylab="",col="red")
x <- 1:20
y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,
-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
plot(x,y,type="n",main="")
abline(h=c(-5,5),col="red",lty=2,lwd=2)
segments(x0=c(5,15),y0=c(-5,-5),x1=c(5,15),y1=c(5,5),col="red",lty=3,
lwd=2)
points(x[y>=5],y[y>=5],pch=4,col="darkmagenta",cex=2)
points(x[y<=-5],y[y<=-5],pch=3,col="darkgreen",cex=2)
points(x[(x>=5&x<=15)&(y>-5&y<5)],y[(x>=5&x<=15)&(y>-5&y<5)],pch=19,
col="blue")
points(x[(x<5|x>15)&(y>-5&y<5)],y[(x<5|x>15)&(y>-5&y<5)])
lines(x,y,lty=4)
arrows(x0=8,y0=14,x1=11,y1=2.5)
text(x=8,y=15,labels="sweet spot")
legend("bottomleft",
legend=c("overall process","sweet","standard",
"too big","too small","sweet y range","sweet x range"),
pch=c(NA,19,1,4,3,NA,NA),lty=c(4,NA,NA,NA,NA,2,3),
col=c("black","blue","black","darkmagenta","darkgreen","red","red"),
lwd=c(1,NA,NA,NA,NA,2,2),pt.cex=c(NA,1,1,2,2,NA,NA))
mydata_txt <- read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
header=TRUE,
sep=";",
na.strings="-9999")
mydata_txt
plot(mydata_txt$temp, type="l" )
File - New Project - New Directory
New Project
Directory Name - Create Project
File - New File - Script
Arithmetic Operators ( + - * / ^ )
Scientific Math
Assignment Arrow (<-)
a <- 3.8
# value <- old_code()
value <- new_code()
a <- 4
b <- 3:9
c <- 7L
d <- 1i
e <- 5 < -9
f <- "23"
date <- "2007-06-22"
date1 <- as.Date(date)
date2 <- as.Date("2004-02-13")
date1 - date2
## Time difference of 1225 days
c()
spring_months <- c("March", "April", "May", "June")
spring_months[2] <- "new"
spring_months
## [1] "March" "new" "May" "June"
myvec1 <- c(1, 3, 1, 42)
a <- 35
myvec2 <- c(3L, myvec1, 1e+03, 64^0.5, 2+(3-1.1)/9.44, a)
myvec2
## [1] 3.000000 1.000000 3.000000 1.000000 42.000000 1000.000000
## [7] 8.000000 2.201271 35.000000
?lenght ?seq ?round ?rep ?sort ?runif ?set.seed
matrix(data = ,nrow = ,ncol = )
data <- runif(9,1,100)
data
## [1] 75.951628 21.066533 71.401001 13.047500 25.303363 15.187134 24.723312
## [8] 6.834503 64.586538
matrix <- matrix(data, nrow = 3, ncol = 3)
matrix
## [,1] [,2] [,3]
## [1,] 75.95163 13.04750 24.723312
## [2,] 21.06653 25.30336 6.834503
## [3,] 71.40100 15.18713 64.586538
print(c(length(matrix),dim(matrix)))
## [1] 9 3 3
array(data = ,dim = )
data <- 1:24
array <- array(data, dim = c(4,3,2)) # raw, col, level
array
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
array[2,2,1] # raw, col, level
## [1] 6
gender = c("male", "female", "male", "male", "female")
gender
## [1] "male" "female" "male" "male" "female"
class(gender)
## [1] "character"
str(gender)
## chr [1:5] "male" "female" "male" "male" "female"
gender[2]
## [1] "female"
gender_factor <- factor(c("male", "female", "male", "male", "female"))
gender_factor
## [1] male female male male female
## Levels: female male
class(gender_factor)
## [1] "factor"
str(gender_factor)
## Factor w/ 2 levels "female","male": 2 1 2 2 1
gender_factor[2]
## [1] female
## Levels: female male
data.frame(data1,data2,data3…)
person=c("Peter", "Lois", "Meg", "Chris", "Stewie")
age=c(42, 40, 17, 14 ,1)
sex=factor(c("M", "F", "F", "M", "M"))
married=c(TRUE, TRUE, FALSE, FALSE, FALSE)
df <- data.frame(person, age, sex, married)
df
## person age sex married
## 1 Peter 42 M TRUE
## 2 Lois 40 F TRUE
## 3 Meg 17 F FALSE
## 4 Chris 14 M FALSE
## 5 Stewie 1 M FALSE
str(df)
## 'data.frame': 5 obs. of 4 variables:
## $ person : Factor w/ 5 levels "Chris","Lois",..: 4 2 3 1 5
## $ age : num 42 40 17 14 1
## $ sex : Factor w/ 2 levels "F","M": 2 1 1 2 2
## $ married: logi TRUE TRUE FALSE FALSE FALSE
data.frame(data1,data2,data3…)
person=c("Peter", "Lois", "Meg", "Chris", "Stewie")
age=c(42, 40, 17, 14 ,1)
sex=factor(c("M", "F", "F", "M", "M"))
married=c(TRUE, TRUE, FALSE, FALSE, FALSE)
df <- data.frame(person ,age, sex, married, stringsAsFactors=FALSE)
df
## person age sex married
## 1 Peter 42 M TRUE
## 2 Lois 40 F TRUE
## 3 Meg 17 F FALSE
## 4 Chris 14 M FALSE
## 5 Stewie 1 M FALSE
str(df)
## 'data.frame': 5 obs. of 4 variables:
## $ person : chr "Peter" "Lois" "Meg" "Chris" ...
## $ age : num 42 40 17 14 1
## $ sex : Factor w/ 2 levels "F","M": 2 1 1 2 2
## $ married: logi TRUE TRUE FALSE FALSE FALSE
data.frame(data1,data2,data3…)
df[1]
## person
## 1 Peter
## 2 Lois
## 3 Meg
## 4 Chris
## 5 Stewie
df[[1]] # df$person
## [1] "Peter" "Lois" "Meg" "Chris" "Stewie"
df[[1]][1]
## [1] "Peter"
list(data1,data2,data3…)
matrix <- matrix(data=1:4,nrow=2,ncol=2)
vector <- c(T,F,T,T)
var <- "hello"
data_frame <- data.frame(person ,age, sex, married, stringsAsFactors=FALSE)
list <- list(matrix,vector,var,data_frame)
list
## [[1]]
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
##
## [[2]]
## [1] TRUE FALSE TRUE TRUE
##
## [[3]]
## [1] "hello"
##
## [[4]]
## person age sex married
## 1 Peter 42 M TRUE
## 2 Lois 40 F TRUE
## 3 Meg 17 F FALSE
## 4 Chris 14 M FALSE
## 5 Stewie 1 M FALSE
NA, NaN, NULL, Inf
class(NA) # Not Available (“missing” entity)
## [1] "logical"
class(NaN) # Not a Number
## [1] "numeric"
class(NULL) # Null (“empty” entity)
## [1] "NULL"
class(Inf) # Infinity
## [1] "numeric"
person=c("Peter", "Lois", "Meg", "Chris", "Stewie")
age=c(42, 40, 17, 14 ,1)
sex=factor(c("M", "F", "F", "M", "M"))
married=c(TRUE, TRUE, FALSE, FALSE, FALSE)
data_frame <- data.frame(person ,age, sex, married, stringsAsFactors=FALSE)
data_frame
## person age sex married
## 1 Peter 42 M TRUE
## 2 Lois 40 F TRUE
## 3 Meg 17 F FALSE
## 4 Chris 14 M FALSE
## 5 Stewie 1 M FALSE
data_frame[2]
## age
## 1 42
## 2 40
## 3 17
## 4 14
## 5 1
attributes(data_frame)
## $names
## [1] "person" "age" "sex" "married"
##
## $class
## [1] "data.frame"
##
## $row.names
## [1] 1 2 3 4 5
attr(data_frame,"row.names") <- c("bir", "iki", "uc", "dort","bes")
data_frame
## person age sex married
## bir Peter 42 M TRUE
## iki Lois 40 F TRUE
## uc Meg 17 F FALSE
## dort Chris 14 M FALSE
## bes Stewie 1 M FALSE
mydata <- read.csv(file = "Presentation/18397_Cekmekoy_Omerli_15dk.txt",
header = TRUE,
sep = ";")
mydata
## sta_no year month day hour minutes temp precipitation pressure
## 1 18397 2017 7 26 18 0 23.9 0.00 1003.0
## 2 18397 2017 7 26 18 15 23.9 0.00 1003.1
## 3 18397 2017 7 26 18 30 23.8 0.00 1003.2
## 4 18397 2017 7 26 18 45 23.8 0.00 1003.2
## 5 18397 2017 7 26 19 0 23.6 0.00 1003.2
## 6 18397 2017 7 26 19 15 23.2 0.00 1003.1
## 7 18397 2017 7 26 19 30 23.2 0.00 1003.1
## 8 18397 2017 7 26 19 45 23.1 0.00 1003.1
## 9 18397 2017 7 26 20 0 23.0 0.00 1003.1
## 10 18397 2017 7 26 20 15 22.8 0.00 1003.0
## 11 18397 2017 7 26 20 30 22.5 0.00 1003.0
## 12 18397 2017 7 26 20 45 22.4 0.00 1003.0
## 13 18397 2017 7 26 21 0 22.2 0.00 1003.0
## 14 18397 2017 7 26 21 15 22.3 0.00 1003.0
## 15 18397 2017 7 26 21 30 22.2 0.00 1003.1
## 16 18397 2017 7 26 21 45 21.7 0.00 1003.1
## 17 18397 2017 7 26 22 0 21.9 0.00 1003.2
## 18 18397 2017 7 26 22 15 21.7 0.00 1003.3
## 19 18397 2017 7 26 22 30 21.6 0.00 1003.3
## 20 18397 2017 7 26 22 45 22.2 0.00 1003.4
## 21 18397 2017 7 26 23 0 22.2 0.00 1003.4
## 22 18397 2017 7 26 23 15 22.1 0.00 1003.5
## 23 18397 2017 7 26 23 30 22.3 0.00 1003.4
## 24 18397 2017 7 26 23 45 22.5 0.00 1003.4
## 25 18397 2017 7 27 0 0 22.3 0.00 1003.4
## 26 18397 2017 7 27 0 15 22.2 0.00 1003.2
## 27 18397 2017 7 27 0 30 22.5 0.00 1003.2
## 28 18397 2017 7 27 0 45 22.6 0.00 1003.2
## 29 18397 2017 7 27 1 0 22.6 0.00 1003.3
## 30 18397 2017 7 27 1 15 22.6 0.00 1003.4
## 31 18397 2017 7 27 1 30 22.6 0.00 1003.2
## 32 18397 2017 7 27 1 45 22.7 0.00 1003.2
## 33 18397 2017 7 27 2 0 22.6 0.00 1003.3
## 34 18397 2017 7 27 2 15 22.5 0.00 1003.2
## 35 18397 2017 7 27 2 30 22.6 0.00 1003.2
## 36 18397 2017 7 27 2 45 22.5 0.00 1003.1
## 37 18397 2017 7 27 3 0 22.5 0.00 1003.1
## 38 18397 2017 7 27 3 15 22.4 0.00 1003.0
## 39 18397 2017 7 27 3 30 22.5 0.00 1003.1
## 40 18397 2017 7 27 3 45 22.4 0.00 1003.3
## 41 18397 2017 7 27 4 0 22.5 0.00 1003.4
## 42 18397 2017 7 27 4 15 22.6 0.00 1003.5
## 43 18397 2017 7 27 4 30 23.0 0.00 1003.5
## 44 18397 2017 7 27 4 45 23.2 0.00 1003.5
## 45 18397 2017 7 27 5 0 24.2 0.00 1003.6
## 46 18397 2017 7 27 5 15 25.1 0.00 1003.5
## 47 18397 2017 7 27 5 30 25.5 0.00 1003.4
## 48 18397 2017 7 27 5 45 26.1 0.00 1003.3
## 49 18397 2017 7 27 6 0 27.1 0.00 1003.3
## 50 18397 2017 7 27 6 15 26.9 0.00 1003.3
## 51 18397 2017 7 27 6 30 27.6 0.00 1003.3
## 52 18397 2017 7 27 6 45 28.0 0.00 1003.2
## 53 18397 2017 7 27 7 0 28.4 0.00 1003.1
## 54 18397 2017 7 27 7 15 28.5 0.00 1003.1
## 55 18397 2017 7 27 7 30 29.3 0.00 1003.0
## 56 18397 2017 7 27 7 45 30.2 0.00 1002.9
## 57 18397 2017 7 27 8 0 30.1 0.00 1002.8
## 58 18397 2017 7 27 8 15 30.1 0.00 1002.8
## 59 18397 2017 7 27 8 30 30.4 0.00 1002.8
## 60 18397 2017 7 27 8 45 30.4 0.00 1002.8
## 61 18397 2017 7 27 9 0 30.8 0.00 1002.9
## 62 18397 2017 7 27 9 15 30.9 0.00 1002.8
## 63 18397 2017 7 27 9 30 31.0 0.00 1002.6
## 64 18397 2017 7 27 9 45 31.5 0.00 1002.6
## 65 18397 2017 7 27 10 0 31.2 0.00 1002.6
## 66 18397 2017 7 27 10 15 30.9 0.00 1002.4
## 67 18397 2017 7 27 10 30 30.9 0.00 1002.4
## 68 18397 2017 7 27 10 45 30.4 0.00 1002.3
## 69 18397 2017 7 27 11 0 30.4 0.00 1002.1
## 70 18397 2017 7 27 11 15 30.0 0.00 1001.9
## 71 18397 2017 7 27 11 30 29.2 0.00 1001.9
## 72 18397 2017 7 27 11 45 29.5 0.00 1001.7
## 73 18397 2017 7 27 12 0 29.4 0.00 1001.6
## 74 18397 2017 7 27 12 15 29.3 0.00 1001.3
## 75 18397 2017 7 27 12 30 29.6 0.00 1001.2
## 76 18397 2017 7 27 12 45 28.8 0.00 1001.3
## 77 18397 2017 7 27 13 0 29.0 0.00 1001.1
## 78 18397 2017 7 27 13 15 29.0 0.00 1001.2
## 79 18397 2017 7 27 13 30 29.2 0.00 1001.3
## 80 18397 2017 7 27 13 45 28.4 0.00 1001.5
## 81 18397 2017 7 27 14 0 27.8 0.00 1001.6
## 82 18397 2017 7 27 14 15 27.4 0.00 1001.6
## 83 18397 2017 7 27 14 30 26.6 0.00 1001.5
## 84 18397 2017 7 27 14 45 26.2 0.00 1001.2
## 85 18397 2017 7 27 15 0 25.8 0.00 1001.1
## 86 18397 2017 7 27 15 15 25.6 0.00 1001.0
## 87 18397 2017 7 27 15 30 25.4 0.00 1000.9
## 88 18397 2017 7 27 15 45 24.2 0.00 1001.8
## 89 18397 2017 7 27 16 0 19.2 7.01 1003.7
## 90 18397 2017 7 27 16 15 19.5 15.81 1003.2
## 91 18397 2017 7 27 16 30 20.1 16.06 1003.1
## 92 18397 2017 7 27 16 45 20.8 16.06 1003.7
## 93 18397 2017 7 27 17 0 21.2 17.19 -9999.0
## 94 18397 2017 7 27 17 15 21.4 17.21 1005.6
## 95 18397 2017 7 27 17 30 21.4 18.46 1005.4
## 96 18397 2017 7 27 17 45 21.4 21.21 1005.1
## 97 18397 2017 7 27 18 0 21.2 21.21 1005.1
## 98 18397 2017 7 27 18 15 21.0 21.21 -9999.0
## 99 18397 2017 7 27 18 30 20.8 21.21 1006.3
## 100 18397 2017 7 27 18 45 20.9 21.21 -9999.0
## 101 18397 2017 7 27 19 0 20.8 21.40 1005.7
## 102 18397 2017 7 27 19 15 20.7 21.40 1006.2
## 103 18397 2017 7 27 19 30 20.8 21.60 1003.6
## 104 18397 2017 7 27 19 45 20.8 21.82 1003.7
## 105 18397 2017 7 27 20 0 20.9 21.82 -9999.0
## 106 18397 2017 7 27 20 15 20.6 21.82 -9999.0
## 107 18397 2017 7 27 20 30 20.6 21.82 1005.1
## 108 18397 2017 7 27 20 45 20.5 21.82 1005.6
## 109 18397 2017 7 27 21 0 20.7 21.82 1005.5
## 110 18397 2017 7 27 21 15 20.8 21.82 1005.7
## 111 18397 2017 7 27 21 30 20.4 21.82 1005.6
## 112 18397 2017 7 27 21 45 20.4 21.82 1005.8
## 113 18397 2017 7 27 22 0 20.6 21.82 1005.8
## 114 18397 2017 7 27 22 15 20.5 21.82 1005.9
## 115 18397 2017 7 27 22 30 20.4 21.82 1006.0
## 116 18397 2017 7 27 22 45 20.5 21.82 1005.9
## 117 18397 2017 7 27 23 0 20.5 21.82 1005.9
## 118 18397 2017 7 27 23 15 20.6 21.82 1005.9
## 119 18397 2017 7 27 23 30 20.5 21.82 1006.0
## 120 18397 2017 7 27 23 45 20.5 21.82 1006.0
## 121 18397 2017 7 28 0 0 20.4 21.82 1006.0
## relative_humidity
## 1 94
## 2 95
## 3 96
## 4 96
## 5 96
## 6 97
## 7 97
## 8 98
## 9 98
## 10 98
## 11 98
## 12 99
## 13 99
## 14 99
## 15 99
## 16 99
## 17 99
## 18 99
## 19 99
## 20 100
## 21 100
## 22 100
## 23 100
## 24 100
## 25 100
## 26 100
## 27 100
## 28 100
## 29 100
## 30 100
## 31 100
## 32 100
## 33 100
## 34 100
## 35 100
## 36 100
## 37 100
## 38 100
## 39 100
## 40 100
## 41 100
## 42 100
## 43 100
## 44 100
## 45 100
## 46 97
## 47 84
## 48 82
## 49 79
## 50 78
## 51 78
## 52 76
## 53 76
## 54 75
## 55 73
## 56 65
## 57 57
## 58 60
## 59 53
## 60 52
## 61 51
## 62 51
## 63 50
## 64 53
## 65 52
## 66 57
## 67 58
## 68 59
## 69 60
## 70 61
## 71 65
## 72 66
## 73 67
## 74 66
## 75 68
## 76 70
## 77 68
## 78 69
## 79 69
## 80 71
## 81 72
## 82 72
## 83 77
## 84 79
## 85 80
## 86 82
## 87 84
## 88 79
## 89 99
## 90 100
## 91 100
## 92 100
## 93 100
## 94 100
## 95 100
## 96 100
## 97 100
## 98 100
## 99 100
## 100 100
## 101 100
## 102 100
## 103 100
## 104 100
## 105 100
## 106 100
## 107 100
## 108 100
## 109 100
## 110 100
## 111 100
## 112 100
## 113 100
## 114 100
## 115 100
## 116 100
## 117 100
## 118 100
## 119 100
## 120 100
## 121 100
class(mydata)
## [1] "data.frame"
str(mydata)
## 'data.frame': 121 obs. of 10 variables:
## $ sta_no : int 18397 18397 18397 18397 18397 18397 18397 18397 18397 18397 ...
## $ year : int 2017 2017 2017 2017 2017 2017 2017 2017 2017 2017 ...
## $ month : int 7 7 7 7 7 7 7 7 7 7 ...
## $ day : int 26 26 26 26 26 26 26 26 26 26 ...
## $ hour : int 18 18 18 18 19 19 19 19 20 20 ...
## $ minutes : int 0 15 30 45 0 15 30 45 0 15 ...
## $ temp : num 23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23 22.8 ...
## $ precipitation : num 0 0 0 0 0 0 0 0 0 0 ...
## $ pressure : num 1003 1003 1003 1003 1003 ...
## $ relative_humidity: int 94 95 96 96 96 97 97 98 98 98 ...
attributes(mydata)
## $names
## [1] "sta_no" "year" "month"
## [4] "day" "hour" "minutes"
## [7] "temp" "precipitation" "pressure"
## [10] "relative_humidity"
##
## $class
## [1] "data.frame"
##
## $row.names
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
## [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
## [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
## [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
## [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
## [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
## [103] 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
## [120] 120 121
temp_data <- mydata$temp
temp_data
## [1] 23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23.0 22.8 22.5 22.4 22.2 22.3
## [15] 22.2 21.7 21.9 21.7 21.6 22.2 22.2 22.1 22.3 22.5 22.3 22.2 22.5 22.6
## [29] 22.6 22.6 22.6 22.7 22.6 22.5 22.6 22.5 22.5 22.4 22.5 22.4 22.5 22.6
## [43] 23.0 23.2 24.2 25.1 25.5 26.1 27.1 26.9 27.6 28.0 28.4 28.5 29.3 30.2
## [57] 30.1 30.1 30.4 30.4 30.8 30.9 31.0 31.5 31.2 30.9 30.9 30.4 30.4 30.0
## [71] 29.2 29.5 29.4 29.3 29.6 28.8 29.0 29.0 29.2 28.4 27.8 27.4 26.6 26.2
## [85] 25.8 25.6 25.4 24.2 19.2 19.5 20.1 20.8 21.2 21.4 21.4 21.4 21.2 21.0
## [99] 20.8 20.9 20.8 20.7 20.8 20.8 20.9 20.6 20.6 20.5 20.7 20.8 20.4 20.4
## [113] 20.6 20.5 20.4 20.5 20.5 20.6 20.5 20.5 20.4
plot(temp_data)
print(min(temp_data))
## [1] 19.2
which(temp_data==min(temp_data))
## [1] 89
which(temp_data==19.2)
## [1] 89
temp_data[which(temp_data==min(temp_data))] <- NA
temp_data[which(temp_data==19.2)] <- NA
temp_data[89] <- NA
print(temp_data)
## [1] 23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23.0 22.8 22.5 22.4 22.2 22.3
## [15] 22.2 21.7 21.9 21.7 21.6 22.2 22.2 22.1 22.3 22.5 22.3 22.2 22.5 22.6
## [29] 22.6 22.6 22.6 22.7 22.6 22.5 22.6 22.5 22.5 22.4 22.5 22.4 22.5 22.6
## [43] 23.0 23.2 24.2 25.1 25.5 26.1 27.1 26.9 27.6 28.0 28.4 28.5 29.3 30.2
## [57] 30.1 30.1 30.4 30.4 30.8 30.9 31.0 31.5 31.2 30.9 30.9 30.4 30.4 30.0
## [71] 29.2 29.5 29.4 29.3 29.6 28.8 29.0 29.0 29.2 28.4 27.8 27.4 26.6 26.2
## [85] 25.8 25.6 25.4 24.2 NA 19.5 20.1 20.8 21.2 21.4 21.4 21.4 21.2 21.0
## [99] 20.8 20.9 20.8 20.7 20.8 20.8 20.9 20.6 20.6 20.5 20.7 20.8 20.4 20.4
## [113] 20.6 20.5 20.4 20.5 20.5 20.6 20.5 20.5 20.4
plot(temp_data)
mydata$temp <- temp_data
mydata
## sta_no year month day hour minutes temp precipitation pressure
## 1 18397 2017 7 26 18 0 23.9 0.00 1003.0
## 2 18397 2017 7 26 18 15 23.9 0.00 1003.1
## 3 18397 2017 7 26 18 30 23.8 0.00 1003.2
## 4 18397 2017 7 26 18 45 23.8 0.00 1003.2
## 5 18397 2017 7 26 19 0 23.6 0.00 1003.2
## 6 18397 2017 7 26 19 15 23.2 0.00 1003.1
## 7 18397 2017 7 26 19 30 23.2 0.00 1003.1
## 8 18397 2017 7 26 19 45 23.1 0.00 1003.1
## 9 18397 2017 7 26 20 0 23.0 0.00 1003.1
## 10 18397 2017 7 26 20 15 22.8 0.00 1003.0
## 11 18397 2017 7 26 20 30 22.5 0.00 1003.0
## 12 18397 2017 7 26 20 45 22.4 0.00 1003.0
## 13 18397 2017 7 26 21 0 22.2 0.00 1003.0
## 14 18397 2017 7 26 21 15 22.3 0.00 1003.0
## 15 18397 2017 7 26 21 30 22.2 0.00 1003.1
## 16 18397 2017 7 26 21 45 21.7 0.00 1003.1
## 17 18397 2017 7 26 22 0 21.9 0.00 1003.2
## 18 18397 2017 7 26 22 15 21.7 0.00 1003.3
## 19 18397 2017 7 26 22 30 21.6 0.00 1003.3
## 20 18397 2017 7 26 22 45 22.2 0.00 1003.4
## 21 18397 2017 7 26 23 0 22.2 0.00 1003.4
## 22 18397 2017 7 26 23 15 22.1 0.00 1003.5
## 23 18397 2017 7 26 23 30 22.3 0.00 1003.4
## 24 18397 2017 7 26 23 45 22.5 0.00 1003.4
## 25 18397 2017 7 27 0 0 22.3 0.00 1003.4
## 26 18397 2017 7 27 0 15 22.2 0.00 1003.2
## 27 18397 2017 7 27 0 30 22.5 0.00 1003.2
## 28 18397 2017 7 27 0 45 22.6 0.00 1003.2
## 29 18397 2017 7 27 1 0 22.6 0.00 1003.3
## 30 18397 2017 7 27 1 15 22.6 0.00 1003.4
## 31 18397 2017 7 27 1 30 22.6 0.00 1003.2
## 32 18397 2017 7 27 1 45 22.7 0.00 1003.2
## 33 18397 2017 7 27 2 0 22.6 0.00 1003.3
## 34 18397 2017 7 27 2 15 22.5 0.00 1003.2
## 35 18397 2017 7 27 2 30 22.6 0.00 1003.2
## 36 18397 2017 7 27 2 45 22.5 0.00 1003.1
## 37 18397 2017 7 27 3 0 22.5 0.00 1003.1
## 38 18397 2017 7 27 3 15 22.4 0.00 1003.0
## 39 18397 2017 7 27 3 30 22.5 0.00 1003.1
## 40 18397 2017 7 27 3 45 22.4 0.00 1003.3
## 41 18397 2017 7 27 4 0 22.5 0.00 1003.4
## 42 18397 2017 7 27 4 15 22.6 0.00 1003.5
## 43 18397 2017 7 27 4 30 23.0 0.00 1003.5
## 44 18397 2017 7 27 4 45 23.2 0.00 1003.5
## 45 18397 2017 7 27 5 0 24.2 0.00 1003.6
## 46 18397 2017 7 27 5 15 25.1 0.00 1003.5
## 47 18397 2017 7 27 5 30 25.5 0.00 1003.4
## 48 18397 2017 7 27 5 45 26.1 0.00 1003.3
## 49 18397 2017 7 27 6 0 27.1 0.00 1003.3
## 50 18397 2017 7 27 6 15 26.9 0.00 1003.3
## 51 18397 2017 7 27 6 30 27.6 0.00 1003.3
## 52 18397 2017 7 27 6 45 28.0 0.00 1003.2
## 53 18397 2017 7 27 7 0 28.4 0.00 1003.1
## 54 18397 2017 7 27 7 15 28.5 0.00 1003.1
## 55 18397 2017 7 27 7 30 29.3 0.00 1003.0
## 56 18397 2017 7 27 7 45 30.2 0.00 1002.9
## 57 18397 2017 7 27 8 0 30.1 0.00 1002.8
## 58 18397 2017 7 27 8 15 30.1 0.00 1002.8
## 59 18397 2017 7 27 8 30 30.4 0.00 1002.8
## 60 18397 2017 7 27 8 45 30.4 0.00 1002.8
## 61 18397 2017 7 27 9 0 30.8 0.00 1002.9
## 62 18397 2017 7 27 9 15 30.9 0.00 1002.8
## 63 18397 2017 7 27 9 30 31.0 0.00 1002.6
## 64 18397 2017 7 27 9 45 31.5 0.00 1002.6
## 65 18397 2017 7 27 10 0 31.2 0.00 1002.6
## 66 18397 2017 7 27 10 15 30.9 0.00 1002.4
## 67 18397 2017 7 27 10 30 30.9 0.00 1002.4
## 68 18397 2017 7 27 10 45 30.4 0.00 1002.3
## 69 18397 2017 7 27 11 0 30.4 0.00 1002.1
## 70 18397 2017 7 27 11 15 30.0 0.00 1001.9
## 71 18397 2017 7 27 11 30 29.2 0.00 1001.9
## 72 18397 2017 7 27 11 45 29.5 0.00 1001.7
## 73 18397 2017 7 27 12 0 29.4 0.00 1001.6
## 74 18397 2017 7 27 12 15 29.3 0.00 1001.3
## 75 18397 2017 7 27 12 30 29.6 0.00 1001.2
## 76 18397 2017 7 27 12 45 28.8 0.00 1001.3
## 77 18397 2017 7 27 13 0 29.0 0.00 1001.1
## 78 18397 2017 7 27 13 15 29.0 0.00 1001.2
## 79 18397 2017 7 27 13 30 29.2 0.00 1001.3
## 80 18397 2017 7 27 13 45 28.4 0.00 1001.5
## 81 18397 2017 7 27 14 0 27.8 0.00 1001.6
## 82 18397 2017 7 27 14 15 27.4 0.00 1001.6
## 83 18397 2017 7 27 14 30 26.6 0.00 1001.5
## 84 18397 2017 7 27 14 45 26.2 0.00 1001.2
## 85 18397 2017 7 27 15 0 25.8 0.00 1001.1
## 86 18397 2017 7 27 15 15 25.6 0.00 1001.0
## 87 18397 2017 7 27 15 30 25.4 0.00 1000.9
## 88 18397 2017 7 27 15 45 24.2 0.00 1001.8
## 89 18397 2017 7 27 16 0 NA 7.01 1003.7
## 90 18397 2017 7 27 16 15 19.5 15.81 1003.2
## 91 18397 2017 7 27 16 30 20.1 16.06 1003.1
## 92 18397 2017 7 27 16 45 20.8 16.06 1003.7
## 93 18397 2017 7 27 17 0 21.2 17.19 -9999.0
## 94 18397 2017 7 27 17 15 21.4 17.21 1005.6
## 95 18397 2017 7 27 17 30 21.4 18.46 1005.4
## 96 18397 2017 7 27 17 45 21.4 21.21 1005.1
## 97 18397 2017 7 27 18 0 21.2 21.21 1005.1
## 98 18397 2017 7 27 18 15 21.0 21.21 -9999.0
## 99 18397 2017 7 27 18 30 20.8 21.21 1006.3
## 100 18397 2017 7 27 18 45 20.9 21.21 -9999.0
## 101 18397 2017 7 27 19 0 20.8 21.40 1005.7
## 102 18397 2017 7 27 19 15 20.7 21.40 1006.2
## 103 18397 2017 7 27 19 30 20.8 21.60 1003.6
## 104 18397 2017 7 27 19 45 20.8 21.82 1003.7
## 105 18397 2017 7 27 20 0 20.9 21.82 -9999.0
## 106 18397 2017 7 27 20 15 20.6 21.82 -9999.0
## 107 18397 2017 7 27 20 30 20.6 21.82 1005.1
## 108 18397 2017 7 27 20 45 20.5 21.82 1005.6
## 109 18397 2017 7 27 21 0 20.7 21.82 1005.5
## 110 18397 2017 7 27 21 15 20.8 21.82 1005.7
## 111 18397 2017 7 27 21 30 20.4 21.82 1005.6
## 112 18397 2017 7 27 21 45 20.4 21.82 1005.8
## 113 18397 2017 7 27 22 0 20.6 21.82 1005.8
## 114 18397 2017 7 27 22 15 20.5 21.82 1005.9
## 115 18397 2017 7 27 22 30 20.4 21.82 1006.0
## 116 18397 2017 7 27 22 45 20.5 21.82 1005.9
## 117 18397 2017 7 27 23 0 20.5 21.82 1005.9
## 118 18397 2017 7 27 23 15 20.6 21.82 1005.9
## 119 18397 2017 7 27 23 30 20.5 21.82 1006.0
## 120 18397 2017 7 27 23 45 20.5 21.82 1006.0
## 121 18397 2017 7 28 0 0 20.4 21.82 1006.0
## relative_humidity
## 1 94
## 2 95
## 3 96
## 4 96
## 5 96
## 6 97
## 7 97
## 8 98
## 9 98
## 10 98
## 11 98
## 12 99
## 13 99
## 14 99
## 15 99
## 16 99
## 17 99
## 18 99
## 19 99
## 20 100
## 21 100
## 22 100
## 23 100
## 24 100
## 25 100
## 26 100
## 27 100
## 28 100
## 29 100
## 30 100
## 31 100
## 32 100
## 33 100
## 34 100
## 35 100
## 36 100
## 37 100
## 38 100
## 39 100
## 40 100
## 41 100
## 42 100
## 43 100
## 44 100
## 45 100
## 46 97
## 47 84
## 48 82
## 49 79
## 50 78
## 51 78
## 52 76
## 53 76
## 54 75
## 55 73
## 56 65
## 57 57
## 58 60
## 59 53
## 60 52
## 61 51
## 62 51
## 63 50
## 64 53
## 65 52
## 66 57
## 67 58
## 68 59
## 69 60
## 70 61
## 71 65
## 72 66
## 73 67
## 74 66
## 75 68
## 76 70
## 77 68
## 78 69
## 79 69
## 80 71
## 81 72
## 82 72
## 83 77
## 84 79
## 85 80
## 86 82
## 87 84
## 88 79
## 89 99
## 90 100
## 91 100
## 92 100
## 93 100
## 94 100
## 95 100
## 96 100
## 97 100
## 98 100
## 99 100
## 100 100
## 101 100
## 102 100
## 103 100
## 104 100
## 105 100
## 106 100
## 107 100
## 108 100
## 109 100
## 110 100
## 111 100
## 112 100
## 113 100
## 114 100
## 115 100
## 116 100
## 117 100
## 118 100
## 119 100
## 120 100
## 121 100
write.csv(mydata, file = "new_data.csv")
Problem: Take a sample belonged to population, and sum
pop <- 1:6 # This is my population
pop
## [1] 1 2 3 4 5 6
samp <- sample(pop, size = 2) # This is my sample, I choose two var.
samp
## [1] 6 4
sum(samp)
## [1] 10
I want to create a new function named roll
my_new_function <- function() {
new_variable_1 <- # number or something
new_variable_2 <- # number or something
do_this()
}
roll <- function() {
pop <- 1:6
samp <- sample(pop, size = 2)
print(samp)
sum(samp)
}
roll()
## [1] 5 3
## [1] 8
Problem: I want to assign a population spontaneously.
roll_2 <- function() {
pop <-
samp <- sample(pop, size = 2)
print(samp)
sum(samp)
}
roll_2() # This will give error. Because pop in undefined.
roll_2 <- function(pop) {
samp <- sample(pop, size = 2)
print(samp)
sum(samp)
}
roll_2(pop = 1:27)
## [1] 12 22
## [1] 34
You can add new options. { } and () are important
sum(1:27)
## [1] 378
# Think about these functions
# mean(), print(), plot(), max(), install.packages(), help(), ...