-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_json_to_csv.R
60 lines (42 loc) · 1.37 KB
/
weather_json_to_csv.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
library(rjson)
library(data.table)
library(tidyverse)
library(anytime)
# manipulate monthly weather data in jsons into functionable data frame
setwd('C:\\Users\\pucha\\Projects\\circle_of_life')
## function to read json into table
get_data_from_json_into_df <- function(file_name, data){
result <- fromJSON(file = file_name)
all <- as.data.frame(do.call("rbind", result['detail']))
end <- 4 * (length(all) %/% 4)
for (i in c(1:end)){
tmp <- as.data.frame(all[i][[1]]$detail)
data <- bind_rows(data, tmp)
}
return(data)
}
## creating table from all data files
items <- list.files("data_files", pattern='*.json')
data <- data.frame()
for (i in 1:length(items)){
data <- get_data_from_json_into_df(paste('data_files\\',items[i], sep=''), data)
}
## manipulating data
data <- data %>%
select(-icon, -ds, -hl, -hls, -hlsh, -wd) %>%
mutate(ts = parse_time(ts)) %>%
mutate(is_cloudy = grepl('cloud', desc),
is_sunny = grepl('sun', desc),
is_rainy = grepl('rain', desc),
is_snowy = grepl('snow', desc),
is_foggy = grepl('fog', desc),
date = anydate(date/1000)) %>%
select(-desc)
# # common weather features in desc to analyse logical values
# data %>%
# select(desc) %>%
# group_by(desc) %>%
# count() %>%
# arrange(desc(n))
## write to file for later use
write_csv(data, 'weather_data_cumulated.csv')