11649f317674e04d3c98a96a283b13bda32ad27b
[talweg.git] / data / scripts / augment_meteo.R
1 meteo_df = read.csv("meteo.csv")
2
3 #inspiration
4 #http://stackoverflow.com/questions/8214303/conditional-replacement-of-values-in-a-data-frame
5
6 meteo_df$Season = 0
7 meteo_df$Week = 0
8 meteo_df$Pollution = -1
9
10 #Need to load and aggregate PM10 by days: use getData() from package
11 data = getData(..., predict_at=0) #TODO:
12
13 for (i in 1:nrow(meteo_df))
14 {
15 pm10_level = data$getLevel(i)
16 #Fill Pollution column: -1 if no info, 0 to 2 for pollution level
17 if (!is.nan(pm10_level))
18 {
19 if (pm10_level < 30)
20 meteo_df$Pollution[i] = 0
21 else if (pm10_level <= 50)
22 meteo_df$Pollution[i] = 1
23 else #pm10 > 50
24 meteo_df$Pollution[i] = 2
25 }
26
27 #Also fill season + days of week variables
28 meteo_df$Season[i] = ifelse(
29 strsplit(as.character(meteo_df$Date[i]),'/')[[1]][1] %in% c("4","5","6","7","8"),
30 1, 0)
31 current_datetime = strptime(as.character(meteo_df$Date[i]), "%m/%d/%Y", tz="GMT")
32 meteo_df$Week[i] = ifelse(current_datetime$wday %in% c(6,0), 0, 1)
33 }
34
35 #Finally write new data
36 write.csv(meteo_df, file="meteo_extra.csv", row.names=FALSE)