Larry Porter Week 2 Assignment “Data Wrangling with R”

This R markedown file contains the code and output for 5 questions of Assignment 2 of Data WQQrangling with R. In this assignment we were required load packages and to import csv xlsx and txt files in two ways. Import from file on computer and import directly from url. The output will show head() and str() of the imported files

Import and Install Packages

install.packages("readr")
library(readr)

Improve speed of functions over base r package, consistency to importing functions, produce data frames in data.table format for larger data sets, defualt setting removes need to use stringsASFactors,and provide column specification #flexibility, additional arguments to adjusting read in data, and functions to import text files

install.packages("readxl")
library(readxl)

Newest package to access Excel data and works with old and new versions of Excel

install.packages("gdata")
library(gdata)

This packages allows Excel data to be pulled from url’s

install.packages("tidyverse")
install.packages("RSocrata")

Install packages for class

Question 1 Download and Import csv file. Read data and explore.

library(knitr)
redditdata <- read.csv("reddit.csv")
head(redditdata)
##   id gender age.range                           marital.status
## 1  1      0     25-34                                     <NA>
## 2  2      0     25-34                                     <NA>
## 3  3      1     18-24                                     <NA>
## 4  4      0     25-34                                     <NA>
## 5  5      1     25-34                                     <NA>
## 6  6      0     25-34 Married/civil union/domestic partnership
##    employment.status military.service children         education
## 1 Employed full time             <NA>       No Bachelor's degree
## 2 Employed full time             <NA>       No Bachelor's degree
## 3          Freelance             <NA>       No      Some college
## 4          Freelance             <NA>       No Bachelor's degree
## 5 Employed full time             <NA>       No Bachelor's degree
## 6 Employed full time               No       No Bachelor's degree
##         country      state      income.range    fav.reddit      dog.cat
## 1 United States   New York  $150,000 or more  getmotivated         <NA>
## 2 United States   New York  $150,000 or more        gaming         <NA>
## 3 United States   Virginia     Under $20,000 snackexchange         <NA>
## 4 United States   New York  $150,000 or more    spacedicks         <NA>
## 5 United States California $70,000 - $99,999           aww         <NA>
## 6 United States   New York  $150,000 or more        gaming I like dogs.
##    cheese
## 1    <NA>
## 2    <NA>
## 3    <NA>
## 4    <NA>
## 5    <NA>
## 6 Cheddar
str(redditdata)
## 'data.frame':    32754 obs. of  14 variables:
##  $ id               : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ gender           : int  0 0 1 0 1 0 0 0 0 0 ...
##  $ age.range        : Factor w/ 7 levels "18-24","25-34",..: 2 2 1 2 2 2 2 1 3 2 ...
##  $ marital.status   : Factor w/ 6 levels "Engaged","Forever Alone",..: NA NA NA NA NA 4 3 4 4 3 ...
##  $ employment.status: Factor w/ 6 levels "Employed full time",..: 1 1 2 2 1 1 1 4 1 2 ...
##  $ military.service : Factor w/ 2 levels "No","Yes": NA NA NA NA NA 1 1 1 1 1 ...
##  $ children         : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
##  $ education        : Factor w/ 7 levels "Associate degree",..: 2 2 5 2 2 2 5 2 2 5 ...
##  $ country          : Factor w/ 439 levels " Canada"," Canada eh",..: 394 394 394 394 394 394 125 394 394 125 ...
##  $ state            : Factor w/ 53 levels "","Alabama","Alaska",..: 33 33 48 33 6 33 1 6 33 1 ...
##  $ income.range     : Factor w/ 8 levels "$100,000 - $149,999",..: 2 2 8 2 7 2 NA 7 2 7 ...
##  $ fav.reddit       : Factor w/ 1834 levels "","'home' page (or front page if you prefer)",..: 720 691 1511 1528 188 691 1318 571 1629 1 ...
##  $ dog.cat          : Factor w/ 3 levels "I like cats.",..: NA NA NA NA NA 2 2 2 1 1 ...
##  $ cheese           : Factor w/ 11 levels "American","Brie",..: NA NA NA NA NA 3 3 1 10 7 ...

Question 2 Import csv file directly from url. Read data and explore.

url<-"https://bradleyboehmke.github.io/public/data/reddit.csv"
redditurl<-read.csv(url )
head(redditurl)
##   id gender age.range                           marital.status
## 1  1      0     25-34                                     <NA>
## 2  2      0     25-34                                     <NA>
## 3  3      1     18-24                                     <NA>
## 4  4      0     25-34                                     <NA>
## 5  5      1     25-34                                     <NA>
## 6  6      0     25-34 Married/civil union/domestic partnership
##    employment.status military.service children         education
## 1 Employed full time             <NA>       No Bachelor's degree
## 2 Employed full time             <NA>       No Bachelor's degree
## 3          Freelance             <NA>       No      Some college
## 4          Freelance             <NA>       No Bachelor's degree
## 5 Employed full time             <NA>       No Bachelor's degree
## 6 Employed full time               No       No Bachelor's degree
##         country      state      income.range    fav.reddit      dog.cat
## 1 United States   New York  $150,000 or more  getmotivated         <NA>
## 2 United States   New York  $150,000 or more        gaming         <NA>
## 3 United States   Virginia     Under $20,000 snackexchange         <NA>
## 4 United States   New York  $150,000 or more    spacedicks         <NA>
## 5 United States California $70,000 - $99,999           aww         <NA>
## 6 United States   New York  $150,000 or more        gaming I like dogs.
##    cheese
## 1    <NA>
## 2    <NA>
## 3    <NA>
## 4    <NA>
## 5    <NA>
## 6 Cheddar
str(redditurl)
## 'data.frame':    32754 obs. of  14 variables:
##  $ id               : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ gender           : int  0 0 1 0 1 0 0 0 0 0 ...
##  $ age.range        : Factor w/ 7 levels "18-24","25-34",..: 2 2 1 2 2 2 2 1 3 2 ...
##  $ marital.status   : Factor w/ 6 levels "Engaged","Forever Alone",..: NA NA NA NA NA 4 3 4 4 3 ...
##  $ employment.status: Factor w/ 6 levels "Employed full time",..: 1 1 2 2 1 1 1 4 1 2 ...
##  $ military.service : Factor w/ 2 levels "No","Yes": NA NA NA NA NA 1 1 1 1 1 ...
##  $ children         : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
##  $ education        : Factor w/ 7 levels "Associate degree",..: 2 2 5 2 2 2 5 2 2 5 ...
##  $ country          : Factor w/ 439 levels " Canada"," Canada eh",..: 394 394 394 394 394 394 125 394 394 125 ...
##  $ state            : Factor w/ 53 levels "","Alabama","Alaska",..: 33 33 48 33 6 33 1 6 33 1 ...
##  $ income.range     : Factor w/ 8 levels "$100,000 - $149,999",..: 2 2 8 2 7 2 NA 7 2 7 ...
##  $ fav.reddit       : Factor w/ 1834 levels "","'home' page (or front page if you prefer)",..: 720 691 1511 1528 188 691 1318 571 1629 1 ...
##  $ dog.cat          : Factor w/ 3 levels "I like cats.",..: NA NA NA NA NA 2 2 2 1 1 ...
##  $ cheese           : Factor w/ 11 levels "American","Brie",..: NA NA NA NA NA 3 3 1 10 7 ...

Question 3 Download and Import xlsx file. Read data and explore.

library(readxl)
library(gdata)
## gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
## 
## gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
## 
## Attaching package: 'gdata'
## The following object is masked from 'package:stats':
## 
##     nobs
## The following object is masked from 'package:utils':
## 
##     object.size
## The following object is masked from 'package:base':
## 
##     startsWith
exceldata<-read.xls("FY2017_4050_FMR.xlsx")
head(exceldata)
##     fips2010  fips2000 fmr2 fmr0 fmr1 fmr3 fmr4 State       Metro_code
## 1 2300512300        NA 1078  755  851 1454 1579    23 METRO38860MM6400
## 2 6099999999        NA  677  502  506  987 1038    60 NCNTY60999N60999
## 3 6999999999        NA  666  411  498  961 1158    69 NCNTY69999N69999
## 4  100199999 100199999  822  587  682 1054 1425     1 METRO33860M33860
## 5  100399999 100399999  977  807  847 1422 1634     1 METRO19300M19300
## 6  100599999 100599999  671  501  505  839  958     1 NCNTY01005N01005
##                          areaname county CouSub               countyname
## 1 Portland, ME HUD Metro FMR Area     NA  12300        Cumberland County
## 2                  American Samoa    999  99999           American Samoa
## 3        Northern Mariana Islands    999  99999 Northern Mariana Islands
## 4              Montgomery, AL MSA      1  99999           Autauga County
## 5   Daphne-Fairhope-Foley, AL MSA      3  99999           Baldwin County
## 6              Barbour County, AL      5  99999           Barbour County
##           county_town_name pop2010 acs_2016_2 state_alpha fmr_type metro
## 1    Chebeague Island town     341       1109          ME       40     1
## 2           American Samoa   55519        653          AS       40     0
## 3 Northern Mariana Islands   53883        642          MP       40     0
## 4           Autauga County   54571        788          AL       40     1
## 5           Baldwin County  182265        873          AL       40     1
## 6           Barbour County   27457        636          AL       40     0
##   FMR_PCT_Change FMR_Dollar_Change
## 1      0.9720469               -31
## 2      1.0367534                24
## 3      1.0373832                24
## 4      1.0431472                34
## 5      1.1191294               104
## 6      1.0550314                35
str(exceldata)
## 'data.frame':    4769 obs. of  21 variables:
##  $ fips2010         : num  2.3e+09 6.1e+09 7.0e+09 1.0e+08 1.0e+08 ...
##  $ fips2000         : num  NA NA NA 1e+08 1e+08 ...
##  $ fmr2             : int  1078 677 666 822 977 671 866 866 621 621 ...
##  $ fmr0             : int  755 502 411 587 807 501 665 665 491 464 ...
##  $ fmr1             : int  851 506 498 682 847 505 751 751 494 467 ...
##  $ fmr3             : int  1454 987 961 1054 1422 839 1163 1163 853 849 ...
##  $ fmr4             : int  1579 1038 1158 1425 1634 958 1298 1298 856 1094 ...
##  $ State            : int  23 60 69 1 1 1 1 1 1 1 ...
##  $ Metro_code       : Factor w/ 2598 levels "METRO10180M10180",..: 451 2592 2594 384 160 625 55 55 626 627 ...
##  $ areaname         : Factor w/ 2598 levels " Santa Ana-Anaheim-Irvine, CA HUD Metro FMR Area",..: 1903 52 1723 1633 571 122 186 186 263 271 ...
##  $ county           : int  NA 999 999 1 3 5 7 9 11 13 ...
##  $ CouSub           : int  12300 99999 99999 99999 99999 99999 99999 99999 99999 99999 ...
##  $ countyname       : Factor w/ 1961 levels "Abbeville County",..: 462 41 1265 92 99 110 163 178 239 249 ...
##  $ county_town_name : Factor w/ 3175 levels "Abbeville County",..: 533 60 2024 136 149 165 254 277 386 401 ...
##  $ pop2010          : int  341 55519 53883 54571 182265 27457 22915 57322 10914 20947 ...
##  $ acs_2016_2       : int  1109 653 642 788 873 636 840 840 569 569 ...
##  $ state_alpha      : Factor w/ 56 levels "AK","AL","AR",..: 24 4 28 2 2 2 2 2 2 2 ...
##  $ fmr_type         : int  40 40 40 40 40 40 40 40 40 40 ...
##  $ metro            : int  1 0 0 1 1 0 1 1 0 0 ...
##  $ FMR_PCT_Change   : num  0.972 1.037 1.037 1.043 1.119 ...
##  $ FMR_Dollar_Change: int  -31 24 24 34 104 35 26 26 52 52 ...

Question 4 Download and Import .xlxs file directly from url. Read data and explore. Note: Need to install strawberry perl for windows to make this work

library(gdata)
excelurl<-read.xls("http://www.huduser.gov/portal/datasets/fmr/fmr2017/FY2017_4050_FMR.xlsx",perl="C:\\Perl64\\bin\\perl.exe")

head(excelurl)
##     fips2010  fips2000 fmr2 fmr0 fmr1 fmr3 fmr4 State       Metro_code
## 1 2300512300        NA 1078  755  851 1454 1579    23 METRO38860MM6400
## 2 6099999999        NA  677  502  506  987 1038    60 NCNTY60999N60999
## 3 6999999999        NA  666  411  498  961 1158    69 NCNTY69999N69999
## 4  100199999 100199999  822  587  682 1054 1425     1 METRO33860M33860
## 5  100399999 100399999  977  807  847 1422 1634     1 METRO19300M19300
## 6  100599999 100599999  671  501  505  839  958     1 NCNTY01005N01005
##                          areaname county CouSub               countyname
## 1 Portland, ME HUD Metro FMR Area     NA  12300        Cumberland County
## 2                  American Samoa    999  99999           American Samoa
## 3        Northern Mariana Islands    999  99999 Northern Mariana Islands
## 4              Montgomery, AL MSA      1  99999           Autauga County
## 5   Daphne-Fairhope-Foley, AL MSA      3  99999           Baldwin County
## 6              Barbour County, AL      5  99999           Barbour County
##           county_town_name pop2010 acs_2016_2 state_alpha fmr_type metro
## 1    Chebeague Island town     341       1109          ME       40     1
## 2           American Samoa   55519        653          AS       40     0
## 3 Northern Mariana Islands   53883        642          MP       40     0
## 4           Autauga County   54571        788          AL       40     1
## 5           Baldwin County  182265        873          AL       40     1
## 6           Barbour County   27457        636          AL       40     0
##   FMR_PCT_Change FMR_Dollar_Change
## 1      0.9720469               -31
## 2      1.0367534                24
## 3      1.0373832                24
## 4      1.0431472                34
## 5      1.1191294               104
## 6      1.0550314                35
str(excelurl)
## 'data.frame':    4769 obs. of  21 variables:
##  $ fips2010         : num  2.3e+09 6.1e+09 7.0e+09 1.0e+08 1.0e+08 ...
##  $ fips2000         : num  NA NA NA 1e+08 1e+08 ...
##  $ fmr2             : int  1078 677 666 822 977 671 866 866 621 621 ...
##  $ fmr0             : int  755 502 411 587 807 501 665 665 491 464 ...
##  $ fmr1             : int  851 506 498 682 847 505 751 751 494 467 ...
##  $ fmr3             : int  1454 987 961 1054 1422 839 1163 1163 853 849 ...
##  $ fmr4             : int  1579 1038 1158 1425 1634 958 1298 1298 856 1094 ...
##  $ State            : int  23 60 69 1 1 1 1 1 1 1 ...
##  $ Metro_code       : Factor w/ 2598 levels "METRO10180M10180",..: 451 2592 2594 384 160 625 55 55 626 627 ...
##  $ areaname         : Factor w/ 2598 levels " Santa Ana-Anaheim-Irvine, CA HUD Metro FMR Area",..: 1903 52 1723 1633 571 122 186 186 263 271 ...
##  $ county           : int  NA 999 999 1 3 5 7 9 11 13 ...
##  $ CouSub           : int  12300 99999 99999 99999 99999 99999 99999 99999 99999 99999 ...
##  $ countyname       : Factor w/ 1961 levels "Abbeville County",..: 462 41 1265 92 99 110 163 178 239 249 ...
##  $ county_town_name : Factor w/ 3175 levels "Abbeville County",..: 533 60 2024 136 149 165 254 277 386 401 ...
##  $ pop2010          : int  341 55519 53883 54571 182265 27457 22915 57322 10914 20947 ...
##  $ acs_2016_2       : int  1109 653 642 788 873 636 840 840 569 569 ...
##  $ state_alpha      : Factor w/ 56 levels "AK","AL","AR",..: 24 4 28 2 2 2 2 2 2 2 ...
##  $ fmr_type         : int  40 40 40 40 40 40 40 40 40 40 ...
##  $ metro            : int  1 0 0 1 1 0 1 1 0 0 ...
##  $ FMR_PCT_Change   : num  0.972 1.037 1.037 1.043 1.119 ...
##  $ FMR_Dollar_Change: int  -31 24 24 34 104 35 26 26 52 52 ...

Question 5 import the Cincinnati (OHCINCIN.txt) file

CINCIN<-read.table("http://academic.udayton.edu/kissock/http/Weather/gsod95-current/OHCINCIN.txt")
head(CINCIN)
##   V1 V2   V3   V4
## 1  1  1 1995 41.1
## 2  1  2 1995 22.2
## 3  1  3 1995 22.8
## 4  1  4 1995 14.9
## 5  1  5 1995  9.5
## 6  1  6 1995 23.8
str (CINCIN)
## 'data.frame':    7963 obs. of  4 variables:
##  $ V1: int  1 1 1 1 1 1 1 1 1 1 ...
##  $ V2: int  1 2 3 4 5 6 7 8 9 10 ...
##  $ V3: int  1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ...
##  $ V4: num  41.1 22.2 22.8 14.9 9.5 23.8 31.1 26.9 31.3 31.5 ...