---
title: "learning dplyr"
author: "JIASHU MIAO"
date: "2/20/2019"
output: html_document
---

```{r}
pkg <- c("readr","readxl","dplyr","stringr","ggplot2","tidyr")
pkgload <- lapply(pkg, require, character.only = TRUE)
library(hflights)
```

```{r}
head(hflights)
summary(hflights)
```

```{r}
dim(hflights)
```
```{r}
# tbl is a special type of data.frame
```

```{r}
hflights <- tbl_df(hflights)
hflights
```
```{r}
glimpse(hflights)

9%%8
```

```{r}
attach(hflights)

```
```{r}
Dest[Distance]
```


```{r}
two <- c("AA","AS")
lut <- c("AA" = "American",
         "AS"= "Alaska",
         "B6"= "JetBlue")
lut[two]
# assign name into it 
```

```{r}
 # Print out a summary with variable max_div
summarize(filter(hflights, Diverted == 1), max_div = max(Distance))



```

```{r}

```

```{r}
diff(range(1:20))
```

```{r}
# first(x) - The first element of vector x.
# last(x) - The last element of vector x.
# nth(x, n) - The nth element of vector x.
# n() - The number of rows in the data.frame or group of observations that summarize() describes.
# n_distinct(x) - The number of unique values in vector x.


```

```{r}
nth(hflights$Year,2)
```

```{r}
# Print out a summary of hflights with the following variables:
# n_obs: the total number of observations,
# n_carrier: the total number of carriers,
# n_dest: the total number of destinations,
# aa, a tbl with all flights flown by American Airlines, is already available.
```
```{r}
mtcars

# 5. warning=FALSE and message=FALSE are useful for suppressing any messages produced when loading packages
is.na(mtcars
      )
```




```{r}
# ggplot(data = delay, mapping = aes(x = dist, y = delay)) +
#   geom_point(aes(size = count), alpha = 1/3) +
#   geom_smooth(se = FALSE)
# #> `geom_smooth()` using method = 'loess
```

```{r}
#datachiq suqiure
```

```{r}
library(readr)
file_path <- "http://www.sthda.com/sthda/RDoc/data/housetasks.txt."
housetasks <- read.delim(file_path)
head(housetasks)



```

```{r}
diamonds2 <- diamonds %>% 
  mutate(y = ifelse(y < 3 | y > 20, NA, y))
diamonds2$y
sum(is.na(diamonds2$y))
which(is.na(diamonds2$y))

```


```{r}
# install.packages(rbootcamp)
# library(rbootcamp)
```
```{r}
library(tidyverse)
library(dplyr)
library(modelr)
library(broom)
advertising <- read_csv("http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv") %>%
  select(-X1)
glimpse(box)

set.seed(123)


```

```{r}
set.seed(123)
sample <- sample(c(TRUE, FALSE), nrow(advertising), replace = T, prob = c(0.6,0.4))
train <- advertising[sample, ]
test <- advertising[!sample, ]


```


```{r}
s
```

```{r}
rnorm(2)
qnorm(1)
#file.choose()
# runif(1)
```
```{r}

```
















