-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.R
131 lines (91 loc) · 2.04 KB
/
Functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#######FUNCTIONS
##Loop
##If-Else, eg. check condition
a <- 2
if (a>0)
{print("it is a positive number")
} else{
print("number is not positive")
}
if (a==1)
{print("you won 1€")
} else if(a==2){
print("you won 2³")
}else {
print("you loose all")
}
#or
ifelse(a==2, "won","loose")
#for/Repeat -> for loops in R are slow...!!!
for (i in 1:100){
print(i)
}
#While Loop e.g. create buffer around cities until the buffer oberlap
#version1
j <- 0
while (j<1)
{
j <- j+0.1; print (j)
}
#version2
j <- 0
while (j<1){
print (j)
j <- j+0.1;
}
#functions
#myfunction <- function(arg1,arg2,....){
# statements
# return (Object)
#}
#myfunction <- function (x,y){
# z <- y+x
# return(z)
#}
#paste - space betwenn, space0, no spae
greet <- function(name){
paste("how are your",name)
}
greet <- function(name) {glue::glue("how are your,{name}")}
greet("Martin")
fun_ndvi <- function(nir,red) {(nir-red)/(nir+red)}
#calc function to raster calculation, you dont need to extract values
output <- calc(nir,red,fun=fun_ndvi)
##"morning" is default
hello_eagles <- function(name,daytime="morning"){
paste0("hello, ", paste0(eagles, collapse=", "), "!")
}
# Random:: --> see all functions of the package
##Pipe Operators
# %>% - split up nested functions
#log(x)
#is the same as:
#x%>%log()
install.packages("babynames")
library(babynames)
library(dplyr)
temp1 <- filter(babynames,sex=="M",name=="Mary")
temp2 <- select(temp1,5)
temp3 <- sum(temp2)
temp3
babynames%>%filter(sex=="M",name=="Mary")%>%
select(5)%>%
sum
babynames%>%filter(sex=="F",name=="Taylor")%>%
select(6)%>%
sum
###my own plots
##open task-add srtm -> terrain profil?
###
library(plyr)
library(ggplot2)
library(base)
library(scales)
#check: ggplot: https://www.datanovia.com/en/blog/how-to-create-a-ggplot-with-multiple-lines/
getwd()
setwd("C:/Users/Annika/Documents/Git/04_GEO_MB2_ProgrammingGeostatistics")
hiking_db2 <- read.csv("LandUse2.csv", header=TRUE, sep=";")
head(hiking_db2)
hiking_db2%>%filter(Name=="Hutten",Year=="2016")%>%
select(2)%>%
sum