小编Jes*_*ton的帖子

如何在R中存储字符串中的特殊字符

我试图将\R 中的特殊转义字符存储为字符串的一部分。

x = "abcd\efg"

# Error: '\e' is an unrecognized escape in character string starting ""abcd\e"

x = "abcd\\efg"

# Works
Run Code Online (Sandbox Code Playgroud)

问题是,这x实际上是我作为 API Web 调用的一部分传递的密码,因此我需要找到一种方法让字符串存储单斜杠文字,才能使其正常工作。

使用忽略文字命令的示例:

> x = r"(abcd\efg)"

> y = "https://url?password="

> paste(x, y, sep = "")
[1] "abcd\\efg123"

> # What I need is for it to return
[1] "abcd\efg123"

> z = paste(y, x, sep = "")

> z
[1] "https://url?password=abcd\\efg"

> cat(z)
https://url?password=abcd\efg
Run Code Online (Sandbox Code Playgroud)

当我z作为 API 命令的一部分传递时,我收到“错误凭据”消息,因为它是\\ …

string r special-characters

5
推荐指数
1
解决办法
2112
查看次数

R将字符串公式转换为函数

我已经为一些数据拟合了多项式模型,我想从该模型中提取公式以找到其最大值。我能够从 lm 对象中提取一个公式作为字符串,但是我无法从该字符串创建一个可以与优化函数一起使用的新函数。

## function for generating data
f1 = function(x) 1 + x^2 - x^3

## random variable from normal distribution
yran = rnorm(500, 1, .025)

## create data by fitting function f to x points times the random variable
dt = data.frame(x  = seq(.01, 1, .01) * yran, 
                y  = f1(seq(.01, 1, .01))*yran)

## sort data frame by x
dt = dt[order(dt$x, decreasing = FALSE), ]

## plot the generated data
plot(dt, ylim = c(.9, 1.24)) …
Run Code Online (Sandbox Code Playgroud)

r

3
推荐指数
1
解决办法
1558
查看次数

标签 统计

r ×2

special-characters ×1

string ×1