precip2 Data Frame (by time and station)
date <- unique(data2[, 6]) #date
date <- format(sort(date)) #sorting dates and converting from numeric to char string
date2 <- as.Date(date, format = "%Y%m%d") #converting dates into proper date format
station_ids_names <- unique(data2[, 1:2]) #station ids
station_names <- station_ids_names[, 2]
precip2 <- matrix(NA, nrow = length(date),
ncol = length(station_names)) #create empty matrix
precip2 <- as.data.frame(precip2) #matrix to data frame
names(precip2) <- station_names #adding column station names to empty precip data frame
row.names(precip2) <- date #adding row dates to empty precip data frame
for (i in 1:length(station_names)){ #loop goes through all 36 stations
station_ind = data2[, 2] == station_names[i] #row is True for station i and False if not
station_date <- format(data2[station_ind, 6]) #dates for station i converted from numeric to character string
ind <- match(station_date, date) #checking that dates match
precip2[ind, i] <- data2[station_ind, 13] #updating/adding precipitation to precip data
}
write.csv(precip2, "~/Desktop/ENSO/Data_Frames/precip2.csv")
temps_min2 Data Frame (by time and station)
temps_min2 <- matrix(NA, nrow = length(date),
ncol = length(station_names)) #create empty matrix
temps_min2 <- as.data.frame(temps_min2) #matrix to data frame
names(temps_min2) <- station_names #adding column station ids to empty temps_min data frame
row.names(temps_min2) <- date #adding row dates to empty temps_min data frame
for (i in 1:length(station_names)){ #loop goes through all 36 stations
station_ind = data2[, 2] == station_names[i] #row is True for station i and False if not
station_date <- format(data2[station_ind, 6]) #dates for station i converted from numeric to character string
ind <- match(station_date, date) #checking that dates match
temps_min2[ind, i] <- data2[station_ind, 17] #updating/adding minimum temperatures to temps_min data
}
write.csv(temps_min2, "~/Desktop/ENSO/Data_Frames/temps_min2.csv")
temps_max2 Data Frame (by time and station)
temps_max2 <- matrix(NA, nrow = length(date),
ncol = length(station_names)) #create empty matrix
temps_max2 <- as.data.frame(temps_max2) #matrix to data frame
names(temps_max2) <- station_names #adding column station ids to empty temps_max data frame
row.names(temps_max2) <- date #adding row dates to empty temps_max data frame
for (i in 1:length(station_names)){ #loop goes through all 36 stations
station_ind = data2[, 2] == station_names[i] #row is True for station i and False if not
station_date <- format(data2[station_ind, 6]) #dates for station i converted from numeric to char string
ind <- match(station_date, date) #checking that dates match
temps_max2[ind, i] <- data2[station_ind, 16] #updating/adding maximum temperatures to temps_max data
}
write.csv(temps_max2, "~/Desktop/ENSO/Data_Frames/temps_max2.csv")