title: "使用stringr处理字符串"
output: html_notebook
https://jrnold.github.io/r4ds-exercise-solutions/strings.html
library(tidyverse)
library(stringr)
10.2.5 练习
(1) 在没有使用 stringr 的那些代码中,你会经常看到 paste() 和 paste0() 函数,这两个函数的区别是什么?stringr中的哪两个函数与它们是对应的?这些函数处理 NA 的方式有什么不同?
paste("foo", "bar")
paste0("foo", "bar")
str_c("foo", "bar")
#两种方法处理NA的的形式不同
str_c("foo", NA)
paste("foo", NA)
paste0("foo", NA)
(2) 用自己的语言描述一下 str_c() 函数的 sep 和 collapse 参数有什么区别?
The sep argument is the string inserted between arguments to str_c(), while collapse is the string used to separate any elements of the character vector into a character vector of length one.
(3) 使用 str_length() 和 str_sub() 函数提取出一个字符串最中间的字符。如果字符串中的
字符数是偶数,你应该怎么做?
x <- c("a", "abc", "abcd", "abcde", "abcdef")
L <- str_length(x);L
m <- ceiling(L/2);m
str_sub(x, m, m)
(4) str_wrap() 函数的功能是什么?应该在何时使用这个函数?
thanks_path <- file.path(R.home("doc"), "THANKS")
thanks <- str_c(readLines(thanks_path), collapse = "\n")
thanks <- word(thanks, 1, 3, fixed("\n\n"))
cat(str_wrap(thanks), "\n")
cat(str_wrap(thanks, width = 40), "\n")
cat(str_wrap(thanks, width = 60, indent = 2), "\n")
cat(str_wrap(thanks, width = 60, exdent = 2), "\n")
cat(str_wrap(thanks, width = 50, exdent = 2), "\n")
(5) str_trim() 函数的功能是什么?其逆操作是哪个函数?
str_trim(" abc ")
str_trim(" abc ", side = "left")
str_trim(" abc ", side = "right")
str_pad("abc", 1, side = "both")
#> [1] " abc "
str_pad("abc", 4, side = "right")
#> [1] "abc "
str_pad("abc", 4, side = "left",pad = "1")
(6) 编写一个函数将字符向量转换为字符串,例如,将字符向量 c("a", "b", "c") 转换为字符串 a、b 和 c。仔细思考一下,如果给定一个长度为 0、1 或 2 的向量,那么这个函数应该怎么做?
str_commasep <- function(x, delim = ",") {
n <- length(x)
if (n == 0) {
""
} else if (n == 1) {
x
} else if (n == 2) {
# no comma before and when n == 2
str_c(x[[1]], "and", x[[2]], sep = " ")
} else {
# commas after all n - 1 elements
not_last <- str_c(x[seq_len(n - 1)], delim)
# prepend "and" to the last element
last <- str_c("and", x[[n]], sep = " ")
# combine parts with spaces
str_c(c(not_last, last), collapse = " ")
}
}
str_commasep("")
#> [1] ""
str_commasep("a")
#> [1] "a"
str_commasep(c("a", "b"))
#> [1] "a and b"
str_commasep(c("a", "b", "c"))
#> [1] "a, b, and c"
str_commasep(c("a", "b", "c", "d"))
#> [1] "a, b, c, and d"
网友评论