如何使用 R 在引号中嵌入字符串(存储在变量中)?
How to embed a character string (stored in a variable) in quotation marks using R?
如何使用转义符将存储在变量中的字符串用单引号括起来?
我已经给了它一些尝试和错误的方法,但失败了。
为了说明我想在这里实现的一个例子:
from:
"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
to:
'"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"'
提前致谢,ExploreR
proj4string(spatial_data)
[1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# this is the character string i want to embed into quotation marks
input_crs <- paste(\'input_crs\')
Error: '\,' is an unrecognized escape in character string starting ""\,"
一个选项,使用 paste
:
在输入周围添加单引号
from <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
to <- paste0("'", from, "'")
或者,如果您希望您的输入用文字双引号引起来,请使用:
to <- paste0("\"", from, "\"")
或者我们可以使用 sub
:
to <- sub("^(.*)$", "'\1'", from)
您仍然需要用引号引起来,但可以使用双引号而不是单引号,因此以下任一方法都有效。
> input_crs <- '\'input_crs\''
> input_crs
[1] "'input_crs'"
> input_crs_2 <- "input_crs'"
> input_crs_2
[1] "input_crs'"
您不需要为此使用 paste0()
,因为事情没有被合并。如果您使用的是存储了一些东西的变量,您将使用 paste0()
:
> input <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
> paste0("'", input, "'")
[1] "'+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0'"
引号信息 here 说:
Single and double quotes delimit character constants. They can be used
interchangeably but double quotes are preferred (and character
constants are printed using double quotes), so single quotes are
normally only used to delimit character constants containing double
quotes.
Backslash is used to start an escape sequence inside character
constants. Escaping a character not in the following table is an
error.
Single quotes need to be escaped by backslash in single-quoted
strings, and double quotes in double-quoted strings.
如何使用转义符将存储在变量中的字符串用单引号括起来?
我已经给了它一些尝试和错误的方法,但失败了。
为了说明我想在这里实现的一个例子:
from:
"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
to:
'"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"'
提前致谢,ExploreR
proj4string(spatial_data)
[1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# this is the character string i want to embed into quotation marks
input_crs <- paste(\'input_crs\')
Error: '\,' is an unrecognized escape in character string starting ""\,"
一个选项,使用 paste
:
from <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
to <- paste0("'", from, "'")
或者,如果您希望您的输入用文字双引号引起来,请使用:
to <- paste0("\"", from, "\"")
或者我们可以使用 sub
:
to <- sub("^(.*)$", "'\1'", from)
您仍然需要用引号引起来,但可以使用双引号而不是单引号,因此以下任一方法都有效。
> input_crs <- '\'input_crs\''
> input_crs
[1] "'input_crs'"
> input_crs_2 <- "input_crs'"
> input_crs_2
[1] "input_crs'"
您不需要为此使用 paste0()
,因为事情没有被合并。如果您使用的是存储了一些东西的变量,您将使用 paste0()
:
> input <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
> paste0("'", input, "'")
[1] "'+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0'"
引号信息 here 说:
Single and double quotes delimit character constants. They can be used interchangeably but double quotes are preferred (and character constants are printed using double quotes), so single quotes are normally only used to delimit character constants containing double quotes.
Backslash is used to start an escape sequence inside character constants. Escaping a character not in the following table is an error.
Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings.