在 paste 和 paste0 中连接特殊字符串
Concatenating special character strings in paste and paste0
我想知道如何连接 paste0
中的特殊字符串,例如 \item.
paste0("\item", "b")
paste0("a", "b")
#> [1] "ab"
paste0("a", "\b")
#> [1] "a\b"
paste0("\item", "\b")
#> Error: '\i' is an unrecognized escape in character string starting ""\i"
已编辑
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
head(airquality) %>%
mutate(Ozone1 = cat(paste0("\item{", Ozone)))
#> \item{41 \item{36 \item{12 \item{18 \item{NA \item{28
#> Ozone Solar.R Wind Temp Month Day
#> 1 41 190 7.4 67 5 1
#> 2 36 118 8.0 72 5 2
#> 3 12 149 12.6 74 5 3
#> 4 18 313 11.5 62 5 4
#> 5 NA NA 14.3 56 5 5
#> 6 28 NA 14.9 66 5 6
如错误所述,您需要转义反斜杠。为了用单个反斜杠打印它,您需要使用 cat
。否则,它被解释为逃逸。因此,您将无法按原样将其添加到数据框中。例如,
library(dplyr)
iris %>% mutate(new = paste0("\item", "b")) #both backslashes are printed
我们不能在 mutate
中使用 cat
。反斜杠被转义。要定义单个反斜杠,您需要在字符串中使用双反斜杠。
查找更多信息here
我想知道如何连接 paste0
中的特殊字符串,例如 \item.
paste0("\item", "b")
paste0("a", "b")
#> [1] "ab"
paste0("a", "\b")
#> [1] "a\b"
paste0("\item", "\b")
#> Error: '\i' is an unrecognized escape in character string starting ""\i"
已编辑
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
head(airquality) %>%
mutate(Ozone1 = cat(paste0("\item{", Ozone)))
#> \item{41 \item{36 \item{12 \item{18 \item{NA \item{28
#> Ozone Solar.R Wind Temp Month Day
#> 1 41 190 7.4 67 5 1
#> 2 36 118 8.0 72 5 2
#> 3 12 149 12.6 74 5 3
#> 4 18 313 11.5 62 5 4
#> 5 NA NA 14.3 56 5 5
#> 6 28 NA 14.9 66 5 6
如错误所述,您需要转义反斜杠。为了用单个反斜杠打印它,您需要使用 cat
。否则,它被解释为逃逸。因此,您将无法按原样将其添加到数据框中。例如,
library(dplyr)
iris %>% mutate(new = paste0("\item", "b")) #both backslashes are printed
我们不能在 mutate
中使用 cat
。反斜杠被转义。要定义单个反斜杠,您需要在字符串中使用双反斜杠。
查找更多信息here