library(tidyverse)
library(tidyverse)
library(igraph) # This is the package to analyze the network
library(visNetwork) # Creates visualizations of the network
library(DT)
library(plotly)
Steps to creating a survey and importing data to R
my_survey <- read_csv("https://docs.google.com/spreadsheets/d/16Pg4U6jrjGN1rPcrzeAnLLYCQWwkZMgVYbPvoP-_sDw/export?format=csv")
Parsed with column specification:
cols(
Timestamp = [31mcol_character()[39m,
`How proud are you to live in your neighborhood?` = [31mcol_character()[39m,
`How many of your neighbors do you know?` = [31mcol_character()[39m,
`How strong is the sense of community in your neighborhood?` = [31mcol_character()[39m,
`How often do you visit public parks in your neighborhood?` = [31mcol_character()[39m,
`Overall, how clean is your neighborhood?` = [31mcol_character()[39m,
`Overall, how safe do you feel in your neighborhood?` = [31mcol_character()[39m,
`How promising is the future of your neighborhood?` = [31mcol_character()[39m,
`How often do you participate in activities in your neighborhood?` = [31mcol_character()[39m,
`Have you ever witnessed a crime in your neighborhood?` = [31mcol_character()[39m,
`Do you think police do a good job with controlling crimes in your neighborhood` = [31mcol_character()[39m,
`How much confidence do you have in the capability of the police to protect you from crime?` = [31mcol_character()[39m,
`Do you consider your area a high, medium or low crime area?` = [31mcol_character()[39m,
`How frequently do you see police patrols in your area?` = [31mcol_character()[39m,
`Are you male or female` = [31mcol_character()[39m,
`What is the highest level of education you have completed` = [31mcol_character()[39m
)
glimpse(my_survey)
Observations: 37
Variables: 16
$ Timestamp [3m[90m<chr>[39m[23m …
$ `How proud are you to live in your neighborhood?` [3m[90m<chr>[39m[23m …
$ `How many of your neighbors do you know?` [3m[90m<chr>[39m[23m …
$ `How strong is the sense of community in your neighborhood?` [3m[90m<chr>[39m[23m …
$ `How often do you visit public parks in your neighborhood?` [3m[90m<chr>[39m[23m …
$ `Overall, how clean is your neighborhood?` [3m[90m<chr>[39m[23m …
$ `Overall, how safe do you feel in your neighborhood?` [3m[90m<chr>[39m[23m …
$ `How promising is the future of your neighborhood?` [3m[90m<chr>[39m[23m …
$ `How often do you participate in activities in your neighborhood?` [3m[90m<chr>[39m[23m …
$ `Have you ever witnessed a crime in your neighborhood?` [3m[90m<chr>[39m[23m …
$ `Do you think police do a good job with controlling crimes in your neighborhood` [3m[90m<chr>[39m[23m …
$ `How much confidence do you have in the capability of the police to protect you from crime?` [3m[90m<chr>[39m[23m …
$ `Do you consider your area a high, medium or low crime area?` [3m[90m<chr>[39m[23m …
$ `How frequently do you see police patrols in your area?` [3m[90m<chr>[39m[23m …
$ `Are you male or female` [3m[90m<chr>[39m[23m …
$ `What is the highest level of education you have completed` [3m[90m<chr>[39m[23m …
You can rename the columns like this:
my_survey <- my_survey %>%
rename(gender = `Are you male or female`) %>%
rename(education = `What is the highest level of education you have completed`) %>%
rename(safe = `Overall, how safe do you feel in your neighborhood?`) %>%
rename(clean = `Overall, how clean is your neighborhood?`) %>%
rename(proud = `How proud are you to live in your neighborhood?`) %>%
rename(police = `How frequently do you see police patrols in your area?`) %>%
rename(witnessedcrime = `Have you ever witnessed a crime in your neighborhood?`) %>%
rename(neighborsknown = `How many of your neighbors do you know?`) %>%
rename(levels = `Do you consider your area a high, medium or low crime area?`) %>%
rename(police2 = `How much confidence do you have in the capability of the police to protect you from crime?`)
glimpse(my_survey)
Observations: 37
Variables: 16
$ Timestamp [3m[90m<chr>[39m[23m …
$ proud [3m[90m<chr>[39m[23m …
$ neighborsknown [3m[90m<chr>[39m[23m …
$ `How strong is the sense of community in your neighborhood?` [3m[90m<chr>[39m[23m …
$ `How often do you visit public parks in your neighborhood?` [3m[90m<chr>[39m[23m …
$ clean [3m[90m<chr>[39m[23m …
$ safe [3m[90m<chr>[39m[23m …
$ `How promising is the future of your neighborhood?` [3m[90m<chr>[39m[23m …
$ `How often do you participate in activities in your neighborhood?` [3m[90m<chr>[39m[23m …
$ witnessedcrime [3m[90m<chr>[39m[23m …
$ `Do you think police do a good job with controlling crimes in your neighborhood` [3m[90m<chr>[39m[23m …
$ police2 [3m[90m<chr>[39m[23m …
$ levels [3m[90m<chr>[39m[23m …
$ police [3m[90m<chr>[39m[23m …
$ gender [3m[90m<chr>[39m[23m …
$ education [3m[90m<chr>[39m[23m …
You won’t be able to get the mean of shy until you recode the responses as numbers.
mean(my_survey$`clean`)
[1] NA
You can recode the verbal labels to numbers, and tell R to treat them as numbers, with this code:
my_survey <- my_survey %>%
mutate(proud = as.numeric(recode(proud,
"Extreemly proud" = "5",
"Very proud" = "4",
"Somewhat proud" = "3",
"Not so proud" = "2",
"Not proud at all" = "1")))
my_survey <- my_survey %>%
mutate(neighborsknown = as.numeric(recode(neighborsknown,
"All of them" = "5",
"Most of them" = "4",
"About half of them" = "3",
"A few of them" = "2",
"None of them" = "1")))
my_survey <- my_survey %>%
mutate(clean = as.numeric(recode(clean,
"Extreemly clean" = "5",
"Very clean" = "4",
"Somewhat clean" = "3",
"Not so clean" = "2",
"Not clean at all" = "1")))
NAs introduced by coercion
my_survey <- my_survey %>%
mutate(police = as.numeric(recode(police,
"I am extremely confident about the police protecting me from a crime" = "5",
"I am vey confident about the police protecting me from a crime" = "4",
"I am somewhat confident about the police protecting me from a crime" = "3",
"I am not confident about the police protecting me from a crime" = "2",
"I dont know" = "1")))
NAs introduced by coercion
my_survey <- my_survey %>%
mutate(education = as.numeric(recode(education,
"College and above" = "5",
"Highschool" = "4",
"Middle school" = "3",
"Elementary school" = "2",
"No formal schooling" = "1")))
my_survey %>%
plot_ly(x = ~gender) %>%
add_histogram()
my_survey %>%
plot_ly(x = ~proud) %>%
add_histogram()
my_survey %>%
plot_ly(x = ~clean) %>%
add_histogram()
Ignoring 8 observationsIgnoring 8 observations
my_survey %>%
count(clean)
my_survey %>%
count(safe)
my_survey %>%
count(police)
my_survey %>%
drop_na(clean) %>%
ggplot(aes(x = safe, fill = clean)) +
geom_bar( position = "fill" ) + scale_fill_viridis_d() + coord_flip()
my_survey %>%
drop_na(safe) %>%
ggplot(aes(x = safe)) +
geom_bar()
my_survey %>%
drop_na(clean) %>%
ggplot(aes(x = clean)) +
geom_bar() +
facet_wrap(vars(safe)) +
coord_flip() +
theme_minimal() +
labs(y = "How safe you feel in your neighborhood",
x = "How clean is your neighborhood",
title = "Safe Vs Clean")
my_survey %>%
drop_na(proud) %>%
ggplot(aes(x = proud)) +
geom_bar() +
facet_wrap(vars(safe)) +
coord_flip() +
theme_minimal() +
labs(y = "How safe you feel",
x = "How proud you are",
title = "Safe Vs proud")
my_survey %>%
drop_na(police) %>%
ggplot(aes(x = police)) +
geom_bar() +
facet_wrap(vars(safe)) +
coord_flip() +
theme_minimal() +
labs(y = "How offten do you see poice ",
x = "How safe you feel in your neighborhood",
title = "Safe Vs police")
my_survey %>%
drop_na(police) %>%
ggplot(aes(x = safe, fill = clean)) +
geom_bar( position = "fill" ) + scale_fill_viridis_d() + coord_flip()
my_survey %>%
drop_na(safe) %>%
ggplot(aes(x = safe)) +
geom_bar() +
facet_wrap(vars(police2)) +
coord_flip() +
theme_minimal() +
labs(y = "How offten do you see police ",
x = "How safe you feel in your neighborhood",
title = "Safe Vs confidence in police")
my_survey %>%
count(education, proud)
my_survey %>%
drop_na(education) %>%
ggplot(aes(x = education)) +
geom_bar()
my_survey %>%
drop_na(education) %>%
ggplot(aes(x = education)) +
geom_bar() +
facet_wrap(vars(proud)) +
coord_flip() +
theme_minimal() +
labs(y = "How Proud to live in your neighborhood ",
x = "Highschool/ College level",
title = "Facebook Users Vs Schooling")