R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

##作业部分

###1.1

a <- data.frame(
  a1 = c(2, 4, 6),
  a2 = c(3, 5, 8),
  a3 = c(4, 1, 9)
)

head(a)
##   a1 a2 a3
## 1  2  3  4
## 2  4  5  1
## 3  6  8  9

###1.2

pmin_result <- pmin(a$a1, a$a2, a$a3)

list(pmin = pmin_result)
## $pmin
## [1] 2 1 6

###1.3

pmin_apply <- function(...) {  
  apply(cbind(...), 1, min)  
}

list(pmin_apply = pmin_apply(a$a1, a$a2, a$a3))
## $pmin_apply
## [1] 2 1 6

###1.4

pmin_for <- function(...) {
  data <- as.data.frame(list(...))
  result <- numeric(nrow(data))
  for (i in 1:nrow(data)) {
    result[i] <- min(data[i, ])
  }
  result
}

list(pmin_for = pmin_for(a))
## $pmin_for
## [1] 2 1 6

###1.5

library(bench)
bench::mark(base=pmin(a$a1,a$a2,a$a3),
            pmin_apply=pmin_apply(a),
            pmin_for=pmin_for(a)
)
## # A tibble: 3 × 6
##   expression      min   median `itr/sec` mem_alloc `gc/sec`
##   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
## 1 base          8.1µs    9.5µs    89230.        0B     17.8
## 2 pmin_apply   99.1µs  106.3µs     8317.    10.2KB     12.5
## 3 pmin_for    345.2µs  357.5µs     2598.        0B     12.7

###2.1

Sys.setlocale(category = "LC_ALL", locale = "Chinese")
## Warning in Sys.setlocale(category = "LC_ALL", locale = "Chinese"): using locale
## code page other than 65001 ("UTF-8") may cause problems
## [1] "LC_COLLATE=Chinese_China.936;LC_CTYPE=Chinese_China.936;LC_MONETARY=Chinese_China.936;LC_NUMERIC=C;LC_TIME=Chinese_China.936"
b1 <- data.frame(
  name_b1 = c("文", "颜", "唐", "黄"),
  score = c(95, 96, 97, 98)
)


b2 <- data.frame(
  name_b2 = c("文", "颜"),
  github = c("uuu1016", "yanyutong111")
)

head(b1)
##   name_b1 score
## 1      文    95
## 2      颜    96
## 3      唐    97
## 4      黄    98
head(b2)
##   name_b2       github
## 1      文      uuu1016
## 2      颜 yanyutong111

###2.2

merged_data <- merge(b1, b2, by.x = "name_b1", by.y = "name_b2")

print(merged_data)
##   name_b1 score       github
## 1      文    95      uuu1016
## 2      颜    96 yanyutong111

###2.3

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
result <- b1 %>%
  inner_join(b2, by = c("name_b1" = "name_b2"))
  
print(result)
##   name_b1 score       github
## 1      文    95      uuu1016
## 2      颜    96 yanyutong111

###2.4

Sys.setlocale(category = "LC_ALL", locale = "Chinese")
## [1] "LC_COLLATE=Chinese_China.936;LC_CTYPE=Chinese_China.936;LC_MONETARY=Chinese_China.936;LC_NUMERIC=C;LC_TIME=Chinese_China.936"
`%merge%` <- function(b1, b2) {
  res <- merge(b1, b2, by.x = "name_b1", by.y = "name_b2")
  rownames(res) <- res$name_b1
  res <- res[, c("score", "github")]
  
  print(res)

}

b1 %merge% b2
##    score       github
## 文    95      uuu1016
## 颜    96 yanyutong111