# SOCIOL359 - Entrepreneurship - Data Expedition
# October 31 & November 5, 2018
# Josh Bruce & Molly Copeland
###Networks of Innovation: Visualizing U.S. patent citation networks in R"
#This script is designed as an introduction to R,
#R Studio, and patent ego-networks for a new user.
#Starting with some R Basics:
# Use '#' symbol to make the text after that a comment
# Comments are helpful description or notes to
#yourself that doesn't get run as code
# To 'run' code: highlight in the script and click
#'Run' or use ctrl+Enter (don't forget to highlight
# or it will re-run from the top)
### 1. Setting the working directory: this is the
#folder R will work from (usually where your data is found)
#Note your directory will not be exactly the same
# as mine! You need to fill it in for where you
# want R to point to
#R code is finicky - if yours isn't working, the
# first things to check are spelling, markers
#like "" , \ () and capitalization, before
#troubleshooting code
#Set Working Directory
setwd("~/Desktop/Soc 359 Data Expedition")
#Note use of "" at beginning and end and / not \ (in Windows)
#We can check if we set it correctly:
getwd()
#Do you see the same filepath name printed in your console?
### 2. Installing and loading packages
# Packages are user-made programs that do fancy stats or make
#pretty visualizations or something else you don't have
#to code from scratch.
# To use a package, you need to INSTALL it first and
# only once, the first time you use it.
# Every time you open R Studio, you need to LOAD any package
# you want to use (think of it like books, you only need to
# buy it once, but you need to put it in your backpack
# each time you want to take it to class).
# Install packages we will use (since this is your
# first time using this package)
# There might be a lot of red text in your console,
# that's ok here, as long as it doesn't say 'ERROR'
# install.packages("igraph")
#Do you see it in your 'packages' tab?
#Now, comment that line out (installing packages
# more than once can break them)
# Load packages
#(This is the step you'll do now and everytime you open R)
library(igraph)
### 3. Loading data
#For now, make sure your data file that you want to have
# R work with is in the working directory you set
# (though you could adapt this code to pull data
# from somewhere else or from the internet)
#Load network data for 3 patent networks:
load("patent_1_edge_list.Rdata")
load("patent_2_edge_list.Rdata")
load("patent_3_edge_list.Rdata")
#Load descriptive info about our 3 patent networks:
load("patent_1_attributes.Rdata")
load("patent_2_attributes.Rdata")
load("patent_3_attributes.Rdata")
#Do you see these 6 files in your environment?
###Now is a good time to save changes to your script
#-- NOT your data, just your code in this script
### 4. Exploring Data
#View column names to figure out what your data include
names(patent_1_edge_list)
names(patent_1_metadata)
#Look at a summary of each variable:
summary(patent_1_metadata)
#What does character mean?
#Or you can view it by double-clicking on the dataset
# in the Environment, or type
View(patent_1_metadata)
#View information about one variable
#Use the format dataset$variable to refer to one
# column in a dataset
table(patent_1_metadata$focal_patent)
#This shows this network has 1 focal patent
#(our main ego), and how many citations, or alters?
#Look at only our focal patent by making a tiny dataset with just that entry:
focalonly