exam

library(stringr)

#1
getFreq <- function(text){
  words <- str_count(text, "\\S+")
  letters  <- str_count(text, "[a-zA-Z']")
  c(words, letters)
}

# 2
res <- lapply(stringr::sentences, getFreq)
s_summary <- do.call("rbind", res)
s_summary <- as.data.frame(s_summary)
colnames(s_summary) <- c("word", "letter")
s_summary$average <- s_summary$letter/s_summary$word

#3
library(ggplot2)
library(tidyverse)
df <- s_summary %>% gather(key="group", value="value", letter:average)
ggplot(df, aes(x=word, y=value, color=group)) + geom_point()

n <- 10000
count <- 0
for(i in 1:n){
  start <- sample(c(0, 5, 10), 1, prob = c(0.7, 0.2, 0.1))
  time <- rnorm(1, 30, 2)
  train_cost <- start + time
  time2 <- sample(c(-32, 30, 32, 34), 1, prob = c(0.3,0.4, 0.2,0.1))
  if(train_cost>time2){
    count <- count + 1
  }
}
count

x <- seq(1, 5, 0.1)
y <- seq(1,5, 0.1)
df <-  expand.grid(x=x, y=y)
df$z<- sin(data$y)/(1+data$x^2)
ggplot(data=df, aes(x, y=y, fill=z)) + geom_tile()
ggplot(df, aes(x=x, y=y, z=z)) + geom_contour()

你可能感兴趣的:(exam)