我正在寻找一种使用R中的正则表达式返回引用的优雅方法.我解释一下:
假设我想找到以月名开头的字符串:
x <- c("May, 1, 2011", "30 June 2011")
grep("May|^June", x, value=TRUE)
[1] "May, 1, 2011"
Run Code Online (Sandbox Code Playgroud)
这有效,但我真的想隔离月份(即"五月",而不是整个匹配的字符串.
因此,可以使用参数gsub返回后向引用substitute.但这有两个问题:
gsub返回原始字符串.这显然不是我想要的:代码和结果:
gsub(".*(^May|^June).*", "\\1", x)
[1] "May" "30 June 2011"
Run Code Online (Sandbox Code Playgroud)
我可以通过执行各种额外的检查来编写解决方法,但这很快变得非常混乱.
为了清楚起见,期望的结果应该是:
[1] "May" NA
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法来实现这一目标?
我正在尝试绘制使用RgoogleMaps包查询的Google地图,并将其与ggplot结合使用.最后,我想显示总人口的使用情况geom_point,有点类似于下图,但是由于过度密谋,我试图集中精力于蒙哥马利地区.
我很沮丧,因为我不能画出我的查询地图中R.我尝试了几个包,如中read.jpeg和png,但它并没有完全制定.
R代码:
#query google map
al1 <- GetMap(center=c(32.362563,-86.304474), zoom=11, destfile = "al2.jpeg",
format="jpg",maptype="roadmap")
#load only specific states
alabama <- subset(all_states, region %in% c("alabama"))
#population
p1 <- ggplot()
p1 <- p1 + geom_polygon(data=alabama,
aes(x=long, y=lat, group=group), colour="white", fill="grey10")
p1 <- p1 + geom_point(data=quote, aes(x=IntPtLon, y=IntPtLat, size=TotPop,
color=TotPop),colour="coral1") + scale_size(name="Total Pop")
Run Code Online (Sandbox Code Playgroud)

编辑:
这是我粗略的结果.我还是想:

al1 <- get_map(location = c(lon = -86.304474, lat = 32.362563), zoom = 11, maptype = 'terrain')
al1MAP <- …Run Code Online (Sandbox Code Playgroud) 我有一个非常大的数据框,大约有1000万行.它有列x和y,我想要的是计算
hypot <- function(x) {sqrt(x[1]^2 + x[2]^2)}
Run Code Online (Sandbox Code Playgroud)
对于每一行.使用apply它需要花费大量时间(大约5分钟,从较小尺寸插值)和内存.
但对我来说似乎太过分了,所以我尝试了不同的东西:
hypot功能可将时间缩短约10%plyr大大增加了运行时间.做这件事的最快方法是什么?
在R中编写函数的常规方法(据我所知)是为了避免副作用并从函数返回一个值.
contained <- function(x) {
x_squared <- x^2
return(x_squared)
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,返回从函数输入计算的值.但是变量x_squared不可用.
但是如果你需要违反这个基本的函数式编程原则(我不确定R对这个问题有多严重)并从函数中返回一个对象,你有两个选择.
escape <- function(x){
x_squared <<- x^2
assign("x_times_x", x*x, envir = .GlobalEnv)
}
Run Code Online (Sandbox Code Playgroud)
这两个对象x_squared,并x_times_x返回.一种方法比另一种方法更好,为什么呢?
有没有办法在R中编码增加的整数序列,类似于使用行程编码(rle)编码运行长度?
我将举例说明:
类比:行程编码
r <- c(rep(1, 4), 2, 3, 4, rep(5, 5))
rle(r)
Run Length Encoding
lengths: int [1:5] 4 1 1 1 5
values : num [1:5] 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
期望:序列长度编码
s <- c(1:4, rep(5, 4), 6:9)
s
[1] 1 2 3 4 5 5 5 5 6 7 8 9
somefunction(s)
Sequence lengths
lengths: int [1:4] 5 1 1 5
value1 : num [1:4] 1 5 5 5
Run Code Online (Sandbox Code Playgroud)
编辑1
因此,somefunction(1:10)将给出结果: …
我正在尝试编写一些简单的Rcpp代码示例.使用Rcpp和inline包装非常容易.
但我很难过如何测试两个字符元素是否相等.以下示例比较了两个字符向量的第一个元素.但我无法编译.
诀窍是什么?
library(Rcpp)
library(inline)
cCode <- '
Rcpp::CharacterVector cx(x);
Rcpp::CharacterVector cy(y);
Rcpp::LogicalVector r(1);
r[0] = (cx[0] == cy[0]);
return(r);
'
cCharCompare <- cxxfunction(signature(x="character", y="character"),
plugin="Rcpp", body=cCode)
cCharCompare("a", "b")
Run Code Online (Sandbox Code Playgroud)
-
==如果两个元素中的一个是常数,则使用的比较完全正常.以下代码编译并给出预期结果:
cCode <- '
Rcpp::CharacterVector cx(x);
Rcpp::LogicalVector r(1);
r[0] = (cx[0] == "a");
return(r);
'
cCharCompareA <- cxxfunction(signature(x="character"), plugin="Rcpp", body=cCode)
cCharCompareA("a")
[1] TRUE
cCharCompareA("b")
[1] FALSE
Run Code Online (Sandbox Code Playgroud) 在我目前正在制作包中的脚本中,需要一些"全局设置".目前,这些设置是全局变量,通常通过直接编辑脚本来更改.(该脚本生成数据库的条目,您需要调整诸如"作者姓名"和条目的其他自定义"常量"部分之类的内容.)
再次,目前我用过const_author <- "Meow The Scientist Cat"等人.当然,我可以完全保留原样,并导出所有全局变量,因此用户可以将它们设置为任何变量.但是,这很丑陋,污染了命名空间.
R中的标准方法是什么使用户可以使用这些设置?使用选项()?在包中的哪个位置应该加载这些选项?
也许使用一个函数settingsTemplate(filename)来导出一个默认设置的文件,然后用户可以自定义; 并且他必须在使用脚本之前获取文件或loadSettings(filename)?
我想用来ggplot2可视化一些功率曲线之后的数据.之前已经问过这个问题(将exp/power trend line添加到ggplot中),但答案并没有真正帮助.
一个技巧是用来stat_function()创建一条曲线.但是,我无法获得 stat_function()和我的功率曲线一起使用对数刻度.
我说明了我的问题.
创建一些示例数据和基础图:
library(ggplot2)
x <- 1:100
pwr <- function(x)x^-2.5
dat <- data.frame(x, y = pwr(x))
p <- ggplot(dat, aes(x = x, y = y)) +
geom_point()
p + stat_function(fun = pwr)
Run Code Online (Sandbox Code Playgroud)
太棒了,让我们用一个增加一些logaritmic scale coord_trans().除了我的直线不再笔直(正如文档告诉我期望的那样)之外,这种方式非常有效.
p + stat_function(fun = pwr) + coord_trans(x = "log10", y = "log10")
Run Code Online (Sandbox Code Playgroud)
所以,再次尝试使用coord_x_log10()和coord_y_log10(),但这会引发错误:
p + stat_function(fun = pwr) + scale_x_log10() + scale_y_log10()
Error in seq.default(min, max, by …Run Code Online (Sandbox Code Playgroud) 我在我的一个R包中有一个简单的函数,其中一个参数symbol = "£":
formatPound <- function(x, digits = 2, nsmall = 2, symbol = "£"){
paste(symbol, format(x, digits = digits, nsmall = nsmall))
}
Run Code Online (Sandbox Code Playgroud)
但是在运行时R CMD check,我收到了这个警告:
* checking R files for non-ASCII characters ... WARNING
Found the following files with non-ASCII characters:
formatters.R
Run Code Online (Sandbox Code Playgroud)
这绝对是£导致问题的符号.如果我用合法的ASCII字符代替它$,警告就会消失.
问题:如何£在我的函数参数中使用,而不会R CMD check发出警告?
我正在尝试安装一个R包,其中包含一些Windows下的C代码.我安装了R 3.3.0,并在计算机的D:盘上安装了Rtools 3.3.0.
而d:\Rtools\bin和d:\Rtools\mingw_64\bin都在PATH.我在命令窗口下检查了gcc命令,它正在工作.
但是当我安装软件包时,它总是试图使用c:/Rtools/mingw_64/bin/gcc,应该在d:磁盘中,然后它会c:/Rtools/mingw_64/bin/gcc: not found出错.
有人可以帮忙解决这个问题吗?
谢谢!