<commit_before>---
title: "Getting & Cleaning Data - Quiz 2"
author: "Aashsih"
date: "Saturday, November 15, 2014"
output: html_document
runtime: shiny
---
------
## Question 1
Register an application with the Github API here https://github.com/settings/applications. Access the API to get information on your instructors repositories (hint: this is the url you want "https://api.github.com/users/jtleek/repos"). Use this data to find the time that the datasharing repo was created. What time was it created? This tutorial may be useful (https://github.com/hadley/httr/blob/master/demo/oauth2-github.r). You may also need to run the code in the base R package and not R studio.

<commit_msg>Completed<commit_after>---
title: "Getting & Cleaning Data - Quiz 2"
author: "Aashsih"
date: "Saturday, November 15, 2014"
output: html_document
runtime: shiny
---
------
## Question 1
Register an application with the Github API here https://github.com/settings/applications. Access the API to get information on your instructors repositories (hint: this is the url you want "https://api.github.com/users/jtleek/repos"). Use this data to find the time that the datasharing repo was created. What time was it created? This tutorial may be useful (https://github.com/hadley/httr/blob/master/demo/oauth2-github.r). You may also need to run the code in the base R package and not R studio.

install.packages("jsonlite")

library(httr)

1. Find OAuth settings for github: http://developer.github.com/v3/oauth/

oauth_endpoints("github")

2. Register an application at https://github.com/settings/applications; Use any URL you would like for the homepage URL (http://github.com is fine) and http://localhost:1410 as the callback url. Insert your client ID and secret below - if secret is omitted, it will look it up in the GITHUB_CONSUMER_SECRET environmental variable.

myapp <- oauth_app("github","a<PASSWORD>","<KEY>")

3. Get OAuth credentials

github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)

4. Use API

gtoken <- config(token = github_token)
req <- GET("https://api.github.com/repos/jtleek/datasharing", gtoken)
stop_for_status(req)
jtleek <- content(req)

5. Print Create Date

jtleek$created_at

### Answer
```{r, echo=FALSE}
print( "2013-11-07T13:25:07Z")

```


## Question 2
The sqldf package allows for execution of SQL commands on R data frames. We will use the sqldf package to practice the queries we might send with the dbSendQuery command in RMySQL. Download the American Community Survey data and load it into an R object called 
acs

https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06pid.csv 

Which of the following commands will select only the data for the probability weights pwgtp1 with ages less than 50?

* sqldf("select * from acs where AGEP < 50 and pwgtp1")
* sqldf("select * from acs")
* sqldf("select * from acs where AGEP < 50")
* sqldf("select pwgtp1 from acs where AGEP < 50")

### Answer 
```{r, echo=FALSE}
  library(sqldf)
  url <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06pid.csv"
  if (! file.exists("data")){
    dir.create("data")
  } 
  datafile <- "data/AmericanCommunitySurvey.csv"

if (! file.exists(datafile)){
    download.file(url , datafile, method = "curl")
  } 

  acs <- read.csv(datafile)

cat("Theare are total ", nrow(acs), " records")
myresult <- sqldf("select pwgtp1 from acs where AGEP < 50")
cat("select pwgtp1 from acs where AGEP < 50 returns ", nrow(myresult), " records")

```

## Question 3

Using the same data frame you created in the previous problem, what is the equivalent function to unique(acs$AGEP)

* sqldf("select distinct AGEP from acs")
* sqldf("select AGEP where unique from acs")
* sqldf("select distinct pwgtp1 from acs")
* sqldf("select unique AGEP from acs")

### Answer 
```{r, echo=FALSE}
  library(sqldf)
  url <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06pid.csv"
  if (! file.exists("data")){
    dir.create("data")
  } 
  datafile <- "data/AmericanCommunitySurvey.csv"

if (! file.exists(datafile)){
    download.file(url , datafile, method = "curl")
  } 

acs <- read.csv(datafile)

cat("unique(acs$AGEP) returns ", length(unique(acs$AGEP)), " records")
myresult <- sqldf("select distinct AGEP from acs")
cat("select distinct AGEP from acs returns ", nrow(myresult), " records")

```

## Question 4 
How many characters are in the 10th, 20th, 30th and 100th lines of HTML from this page: 

http://biostat.jhsph.edu/~jleek/contact.html 

(Hint: the nchar() function in R may be helpful)

* 43 99 8 6
* 45 31 7 31
* 45 0 2 2
* 45 31 7 25
* 45 92 7 2
* 45 31 2 25
* 43 99 7 25

### Answer 

```{r, echo=FALSE}
  url <- "http://biostat.jhsph.edu/~jleek/contact.html"
  if (! file.exists("data")){
    dir.create("data")
  } 
  datafile <- "data/jtleekcontact.html"

if (! file.exists(datafile)){
    download.file(url , datafile, method = "curl")
  } 

data <- readLines("data/jtleekcontact.html")
result <- c()
for ( i in c(10,20,30,100)) {
     result <- c(result, nchar(data[i]))
}
result

```

## Question 5
Read this data set into R and report the sum of the numbers in the fourth of the nine columns. 

https://d396qusza40orc.cloudfront.net/getdata%2Fwksst8110.for 

Original source of the data: http://www.cpc.ncep.noaa.gov/data/indices/wksst8110.for 

(Hint this is a fixed width file format)

* 32426.7
* 222243.1
* 35824.9
* 28893.3
* 101.83
* 36.5

### Answer 

```{r, echo=FALSE}
  url <- "https://d396qusza40orc.cloudfront.net/getdata%2Fwksst8110.for"
  if (! file.exists("data")){
    dir.create("data")
  } 
  datafile <- "data/ClimatePrediction.txt"

if (! file.exists(datafile)){
    download.file(url , datafile, method = "curl")
  } 

x <- read.fwf(datafile,skip=4,widths = c(28,4))
print(sum(x$V2))

```

{'fraction_non_alphanumeric': 0.13255733148019458, 'fraction_numerical': 0.037873523280055596, 'mean_word_length': 4.121886120996441, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 5, 'https://': 13, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '426874', 'n_tokens_mistral': 2188, 'n_tokens_neox': 1954, 'n_words': 625}