# データ解析基礎論B W02

```install.packages("tidyverse")
library(tidyverse)

random.number <- rnorm(1000)
mean(random.number)
mean(random.number <- rnorm(1000))

rnorm(1000) %>% mean()

# CLT
NperSample = 10
SampleSize = 300000

random.number <- runif(NperSample * SampleSize)
dat <- matrix(random.number, nrow=NperSample)
means <- colMeans(dat)
dens <- density(means)
hist(means, breaks = 100, probability = T, main = "Distributionf of Means")
lines(dens, lwd = 3, col = "orange")

runif(NperSample * SampleSize) %>%
matrix(nrow=NperSample) %>%
colMeans() -> means
hist(means, breaks=100,probability = T, main = "Distributionf of Means")
means %>% density() %>% lines(,lwd=3,col='orange')

histWdensity <- function(dat, nbreaks=30, main.txt){
dens <- density(dat)
hist(dat, breaks = nbreaks, probability = T, main = main.txt)
lines(dens, lwd = 3, col = "orange")
}

runif(NperSample * SampleSize) %>%
matrix(nrow=NperSample) %>%
colMeans() %>%
histWdensity(nbreaks = 100, main.txt = "Distributionf of Means")

dt <- as_tibble(dat)
dt.la <- filter(dt, affil == "LA")

dt.la2 <- filter(dt, affil == "LA" & grade == "SP")
dt.laNot2 <- filter(dt, affil == "LA" & grade != "SP")

dtWOgender <- select(dt, -gender)

dt.weekly <- mutate(dt,nbooksWeek = nbooks / 52)

dt.atLeastOneBook <- mutate(dt, atleastOneBook = (nbooks/52) >= 1)
dt.atLeastOneBook <- mutate(dt, atleastOneBook = (nbooks/12) >= 1)

dt.BWindex = mutate(dt, nbooksWeek = nbooks / 52,
idx = nbooksWeek / (12*7-Hworked))

summarize(dt.byGrade, ave.books = mean(nbooks,na.rm = TRUE),
ave.Hworked = mean(Hworked, na.rm = TRUE))

dt.summ <- summarize(dt.byGrAf, ave.books = mean(nbooks,na.rm = TRUE),
ave.Hworked = mean(Hworked, na.rm = TRUE), N = n())

dt.summ2 <- dt %>%
summarize(ave.books = mean(nbooks,na.rm = TRUE),
ave.Hworked = mean(Hworked, na.rm = TRUE),
N = n()) %>% filter(N > 2) %>% arrange(desc(ave.books))

plot(x = dt.summ2\$ave.books, y = dt.summ2\$ave.Hworked, pch=20, cex = 3,
xlab = "Ave. # books read",ylab = "Ave hours worked")

col_names = TRUE)

dt.summ3 <- dt %>%
summarize(ave.books = mean(nbooks,na.rm = TRUE),
ave.Hworked = mean(Hworked, na.rm = TRUE))

dt.summ3G <- dt.summ3 %>% gather('ave.books', 'ave.Hworked',
key = 'ave.values', value = "BorW")

dt.summ3SformG <- spread(dt.summ3G, key = ave.values, value =BorW)

dt.sumLA <- dt %>% filter(affil=="LA") %>% group_by(grade) %>%
summarize(ave.books = mean(nbooks))

toeic <- tibble(
score = c(800,830),
)

new.dt1 <- dt.sumLA %>% inner_join(toeic, by = "grade")

toeic2 <- tibble(
score = c(800,830,900),
)
new.dt3 <- full_join(dt.sumLA, toeic2)

new.dt4 <- left_join(dt.sumLA, toeic2)
new.dt5 <- right_join(dt.sumLA, toeic2)

```