Skip to content

Instantly share code, notes, and snippets.

@wisnunugroho21
Created September 26, 2018 09:19
Show Gist options
  • Save wisnunugroho21/3a01892252f277d4870b29559e3dde5e to your computer and use it in GitHub Desktop.
Save wisnunugroho21/3a01892252f277d4870b29559e3dde5e to your computer and use it in GitHub Desktop.
getNumberFromArray <- function(x, idx) {
return(x[idx])
}
milisecondToTime <- function(x) {
x <- x / 1000
result = as.POSIXct(x, origin = "1970-01-01")
return(result)
}
count_n_range_mag <- function(x, div) {
y <- x %>% group_by(range_mag = cut(mag, breaks = seq(round(min(mag) - 1), round(max(mag)) + 1), by = div)) %>%
summarize(n_mag = n())
return(y)
}
count_n_range_time <- function(x, div) {
y <- x %>% group_by(range_time = cut(time, breaks = seq(min(time) - div, max(time) + div, by = div))) %>%
summarize(n_time = n())
return(y)
}
#install.package("tidyverse")
#install.package("jsonlite")
library(tidyverse)
library(jsonlite)
raw <- fromJSON("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2014-01-01&endtime=2014-01-02")
mag_arr <- raw$features$properties$mag
tsunami_arr <- as.logical(raw$features$properties$tsunami)
place_arr <- raw$features$properties$place
time_arr <- as.POSIXct(raw$features$properties$time / 1000, origin = "1970-01-01")
long_arr <- sapply(raw$features$geometry$coordinates, FUN = getNumberFromArray, idx = 1)
lat_arr <- sapply(raw$features$geometry$coordinates, FUN = getNumberFromArray, idx = 2)
depth_arr <- sapply(raw$features$geometry$coordinates, FUN = getNumberFromArray, idx = 3)
earthquake_df <- data.frame(place = place_arr, mag = mag_arr, tsunami = tsunami_arr, time = time_arr,
long = long_arr, lat = lat_arr, depth = depth_arr)
head(earthquake_df, n = 20)
earthquake_df_subset <- count_n_range_time(earthquake_df, 3 * 60 * 60)
ggplot(earthquake_df_subset, aes(x = range_time, y = n_time)) + geom_col()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment