forked from eikeluedeling/tree_phenology_analysis_in_R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson_6.R
144 lines (99 loc) · 2.62 KB
/
Lesson_6.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
library(chillR)
Winters_hours_gaps
hourtemps<-Winters_hours_gaps[,c("Year","Month","Day","Hour","Temp")]
hourtemps[c(10:20),c(2,3)]
a=1
a<-1
hourtemps[1,"Temp"]<-20
hourtemps[1:5,]
hourtemps[,"myColumn"]<-"Hi"
hourtemps<-hourtemps[,c("Year","Month","Day","Hour","Temp")]
1>2
3<4
1==2
2>=2
2<=3
!(2==2)
(2==2)&(3==3)
(2==3)|(2==3)
a<-3
b<-2
(a==b)
c(1,2,3,4,5)==4
hourtemps$Temp>=0
hourtemps$Temp<=7.2
Harry<-(hourtemps$Temp>=0)&(hourtemps$Temp<=7.2)
Harry
hourtemps[,"Chilling_Hour"]<-(hourtemps$Temp>=0)&(hourtemps$Temp<=7.2)
sum(hourtemps$Chilling_Hour)
Start_Date<-which(hourtemps$Year==2008 &
hourtemps$Month==10 &
hourtemps$Day==1 &
hourtemps$Hour==12)
End_Date<-which(hourtemps$Year==2008 &
hourtemps$Month==10 &
hourtemps$Day==31 &
hourtemps$Hour==12)
1:10
## Sum up chilling hours in October
sum(hourtemps$Chilling_Hour[Start_Date:End_Date])
?sum
test_function<-function(x) x+1
test_function(10)
test_2<-function(x,y)
{z<-x*y
a<-z^4
d<-z+a
return(d)
}
test_2(3,4)
CH<-function(Temp)
{
(Temp>=0)&(Temp<=7.2)
}
CH(2)
CH(8)
hourtemps[,"CH"]<-CH(hourtemps$Temp)
CH_flex<-function(Temp,lower=0,upper=7.2)
{
(Temp>=lower)&(Temp<=upper)
}
hourtemps[,"CH"]<-CH_flex(hourtemps$Temp)
hourtemps[,"CH_5_15"]<-CH_flex(hourtemps$Temp,lower=5,upper=15)
## add start and end dates and make this applicable to data.frame
CH_flex<-function(hourly,Start_Date=NA,End_Date=NA,lower=0,upper=7.2)
{
hourly[,"CH"]<-(hourly$Temp>=lower)&(hourly$Temp<=upper)
return(hourly)
}
hourtemps<-Winters_hours_gaps[,c("Year","Month","Day","Hour","Temp")]
CH_flex(hourtemps)
date<-20080324
date_to_date_components<-function(date)
{
year<-trunc(date/10000)
month<-trunc((date-year*10000)/100)
day<-date-(month*100)-year*10000
return(list(Year=year,Month=month,Day=day,type="Date"))
}
date_to_date_components(20041203)
CH_flex<-function(hourly,Start_Date=NA,End_Date=NA,lower=0,upper=7.2)
{
hourly[,"CH"]<-(hourly$Temp>=lower)&(hourly$Temp<=upper)
start<-date_to_date_components(Start_Date)
end<-date_to_date_components(End_Date)
Start_row<-which(hourly$Year==start$Year &
hourly$Month==start$Month &
hourly$Day==start$Day &
hourly$Hour==12)
End_row<-which(hourly$Year==end$Year &
hourly$Month==end$Month &
hourly$Day==end$Day &
hourly$Hour==12)
CH<-sum(hourly$CH[Start_row:End_row])
return(CH)
}
CH_flex(hourtemps,
Start_Date=20081001,
End_Date=20081031,
lower=10,upper=20)