generated from byuibigdata/project_safegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheda_safegraph.r
131 lines (115 loc) · 4.35 KB
/
eda_safegraph.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
library(tidyverse)
library(sf)
library(jsonlite)
json_to_tibble <- function(x) {
if(is.na(x)) return(x)
parse_json(x) %>%
enframe() %>%
unnest(value)
}
bracket_to_tibble <- function(x){
value <- str_replace_all(x, "\\[|\\]", "") %>%
str_split(",", simplify = TRUE) %>%
as.numeric()
name <- seq_len(length(value))
tibble::tibble(name = name, value = value)
}
dat <- read_csv("SafeGraph - Patterns and Core Data - Chipotle - July 2021/Core Places and Patterns Data/chipotle_core_poi_and_patterns.csv")
dat %>%
slice(5:10) %>%
pull(popularity_by_day)
dat_nest <- dat %>%
slice(1:50) %>% # for the example in class.
mutate(
open_hours = map(open_hours, ~json_to_tibble(.x)),
visits_by_day = map(visits_by_day, ~bracket_to_tibble(.x)),
visitor_country_of_origin = map(
visitor_country_of_origin, ~json_to_tibble(.x)),
bucketed_dwell_times = map(
bucketed_dwell_times, ~json_to_tibble(.x)),
related_same_day_brand = map(
related_same_day_brand, ~json_to_tibble(.x)),
related_same_month_brand = map(
related_same_month_brand, ~json_to_tibble(.x)),
popularity_by_hour = map(
popularity_by_hour, ~json_to_tibble(.x)),
popularity_by_day = map(
popularity_by_day, ~json_to_tibble(.x)),
device_type = map(
device_type, ~json_to_tibble(.x)),
visitor_home_cbgs = map(
visitor_home_cbgs, ~json_to_tibble(.x)),
visitor_home_aggregation = map(
visitor_home_aggregation, ~json_to_tibble(.x)),
visitor_daytime_cbgs = map(
visitor_daytime_cbgs, ~json_to_tibble(.x))
)
dat_nest <- dat_nest %>%
select(placekey, location_name, latitude, longitude, street_address,
city, region, postal_code,
raw_visit_counts:visitor_daytime_cbgs,
parent_placekey, open_hours)
dat_nest %>%
slice(1:5) %>%
select(placekey, location_name, latitude,
longitude, city, region, device_type)
dat %>%
slice(1:5) %>%
select(placekey, location_name, latitude,
longitude, city, region, device_type)
dat_nest %>%
slice(1:5) %>%
filter(!is.na(device_type)) %>%
select(placekey, location_name, latitude, longitude,
city, region, device_type) %>%
unnest(device_type) %>%
filter(!is.na(name)) %>%
pivot_wider(names_from = name, values_from = value)
dat_nest %>%
slice(1:5) %>%
filter(!is.na(device_type)) %>%
select(placekey, location_name, latitude, longitude,
city, region, popularity_by_day) %>%
unnest(popularity_by_day) %>%
filter(!is.na(name)) %>%
pivot_wider(names_from = name, values_from = value)
# This is a problem
dat_nest %>%
slice(1:5) %>%
filter(!is.na(device_type)) %>%
select(placekey, location_name, latitude, longitude,
city, region, device_type, popularity_by_day) %>%
unnest(popularity_by_day) %>%
filter(!is.na(name)) %>%
pivot_wider(names_from = name, values_from = value) %>%
unnest(device_type)
# notice the duplicate rows.
dat_all <- dat %>%
mutate(
open_hours = map(open_hours, ~json_to_tibble(.x)),
visits_by_day = map(visits_by_day, ~bracket_to_tibble(.x)),
visitor_country_of_origin = map(
visitor_country_of_origin, ~json_to_tibble(.x)),
bucketed_dwell_times = map(
bucketed_dwell_times, ~json_to_tibble(.x)),
related_same_day_brand = map(
related_same_day_brand, ~json_to_tibble(.x)),
related_same_month_brand = map(
related_same_month_brand, ~json_to_tibble(.x)),
popularity_by_hour = map(
popularity_by_hour, ~json_to_tibble(.x)),
popularity_by_day = map(
popularity_by_day, ~json_to_tibble(.x)),
device_type = map(
device_type, ~json_to_tibble(.x)),
visitor_home_cbgs = map(
visitor_home_cbgs, ~json_to_tibble(.x)),
visitor_home_aggregation = map(
visitor_home_aggregation, ~json_to_tibble(.x)),
visitor_daytime_cbgs = map(
visitor_daytime_cbgs, ~json_to_tibble(.x))) %>%
select(placekey, latitude, longitude, street_address,
city, region, postal_code,
raw_visit_counts:visitor_daytime_cbgs,
parent_placekey, open_hours)
write_rds(dat_all, "chipotle_nested.rds")