我有一个字符串以及在多个括号内提取信息的内容.目前,我可以使用下面的代码从最后一个括号中提取信息.我该怎么做才能提取多个括号并作为向量返回?
j <- "What kind of cheese isn't your cheese? (wonder) Nacho cheese! (groan) (Laugh)"
sub("\\).*", "", sub(".*\\(", "", j))
Run Code Online (Sandbox Code Playgroud)
目前的输出是:
[1] "Laugh"
Run Code Online (Sandbox Code Playgroud)
期望的输出是:
[1] "wonder" "groan" "Laugh"
Run Code Online (Sandbox Code Playgroud) 我想测试一个字符串,看看哪些元素实际上可以是数字.我可以使用正则表达式来测试整数是否成功,但我希望看到哪些元素具有所有数字和1或更少的小数.以下是我尝试过的内容:
x <- c("0.33", ".1", "3", "123", "2.3.3", "1.2r")
!grepl("[^0-9]", x) #integer test
grepl("[^0-9[\\.{0,1}]]", x) # I know it's wrong but don't know what to do
Run Code Online (Sandbox Code Playgroud)
我正在寻找逻辑输出,所以我希望得到以下结果:
[1] TRUE TRUE TRUE TRUE FALSE FALSE
Run Code Online (Sandbox Code Playgroud) 可能重复:
方程驱动的平滑阴影同心形状
我怎么能在R中绘制一个对称的心脏,就像我绘制一个圆圈(使用plotrix)或一个矩形?
我想要这样的代码,这样我才能真正为自己做这件事,并能够将其推广到类似的未来需求.我已经看到了比这更精细的情节所以它非常可行,只是因为我缺乏这方面的知识.
从R 3.1.0开始,我得到以下R检查:
* checking package dependencies ... NOTE
No repository set, so cyclic dependency check skipped
Run Code Online (Sandbox Code Playgroud)
我试过这个建议:https://twitter.com/phylorich/status/431911660698083328
不行.我将该行options(repos="http://cran.rstudio.com/")放在包根目录中的.Rprofile中.仍然得到注意.
另外,编写R扩展的第1.3.1节说明:
Some Windows users may need to set environment variable R_WIN_NO_JUNCTIONS
to a non-empty value. The test of cyclic declarations33in DESCRIPTION
files needs repositories (including CRAN) set: do this in ~/.Rprofile.
Run Code Online (Sandbox Code Playgroud)
这可能是由于set environment variable R_WIN_NO_JUNCTIONS?如果是这样我怎么能这样做呢?注释的任何其他可能原因或建议的修复?
我有一个对象列表.如何从列表中获取一个对象的名称?如:
LIST <- list(A=1:5, B=1:10)
LIST$A
some.way.cool.function(LIST$A) #function I hope exists
"A" #yay! it has returned what I want
Run Code Online (Sandbox Code Playgroud)
名称(LIST)不正确,因为它返回"A"和"B".
仅针对上下文,我正在绘制一系列存储在列表中的数据帧.当我来到每个data.frame时,我想要包含data.frame的名称作为标题.所以名称(LIST)[1]的答案也不正确.
编辑:我添加了代码以获得更多问题的上下文
x <- c("yes", "no", "maybe", "no", "no", "yes")
y <- c("red", "blue", "green", "green", "orange")
list.xy <- list(x=x, y=y)
WORD.C <- function(WORDS){
require(wordcloud)
L2 <- lapply(WORDS, function(x) as.data.frame(table(x), stringsAsFactors = FALSE))
FUN <- function(X){
windows()
wordcloud(X[, 1], X[, 2], min.freq=1)
mtext(as.character(names(X)), 3, padj=-4.5, col="red") #what I'm trying that isn't working
}
lapply(L2, FUN)
}
WORD.C(list.xy)
Run Code Online (Sandbox Code Playgroud)
如果这有效,名称x和y将在两个图的顶部显示为红色
我最近有一台带有多个内核的计算机,我正在学习使用并行计算.我非常精通lapply并被告知parLapply工作非常相似.我虽然没有正确操作.似乎我必须明确地将所有内容放在其中parLapply以使其工作(即要使用的函数,变量等).使用lapply它从父环境读取,parLapply似乎不这样做.所以在我下面的例子中,我可以通过将所有信息放在里面来使一切工作,parLapply但如果我在用户定义的函数中使用它,我就无法明确地放入text.var其中parLapply.
library(parallel)
text.var <- rep("I like cake and ice cream so much!", 20)
ntv <- length(text.var)
gc.rate <- 10
pos <- function(i) {
paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
}
lapply(seq_len(ntv), function(i) {
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
)
#doesn't work
cl <- makeCluster(mc <- getOption("cl.cores", 4))
parLapply(cl, seq_len(ntv), function(i) {
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
) …Run Code Online (Sandbox Code Playgroud) 这可能是一个愿望清单的事情,不确定(即可能需要为此创建geom_pie).今天我看到了一张地图(LINK),上面有饼图.

我不想讨论饼图的优点,这更多的是我可以在ggplot中做这个练习吗?
我提供了一个下面的数据集(从我的下拉框中加载),其中包含制作纽约州地图的地图数据和一些关于各县种族百分比的纯粹数据.我将这种种族构成作为与主数据集的合并以及作为单独的数据集称为密钥.我也认为布莱恩古德里奇在关于居中县名的另一篇文章(HERE)中对我的回应将有助于这个概念.
我们如何用ggplot2制作上面的地图?
数据集和没有饼图的地图:
load(url("http://dl.dropbox.com/u/61803503/nycounty.RData"))
head(ny); head(key) #view the data set from my drop box
library(ggplot2)
ggplot(ny, aes(long, lat, group=group)) + geom_polygon(colour='black', fill=NA)
# Now how can we plot a pie chart of race on each county
# (sizing of the pie would also be controllable via a size
# parameter like other `geom_` functions).
Run Code Online (Sandbox Code Playgroud)
提前感谢您的想法.
编辑:我刚看到junkcharts的另一个案例,它尖叫着这种类型的能力:

当我使用lapply并打印到控制台时,它会打印出不需要的内容,[[i]]NULL但我希望将预期的消息打印到控制台.我已经尝试了suppressWarnings,suppressMessages但这些并没有删除不受欢迎的罪犯.我搜索lapply并没有看到一个让它沉默的论据.这更美观,因为它不会干扰功能.我不反对在控制台上进行替代打印,只要用户可以根据需要将其关闭即可.
这是一个示例函数,输出和我想得到的:
示例功能:
FUN <- function(x) {
FUN2 <- function(z) message(z)
lapply(1:3, function(i) FUN2(paste(x, i)))
}
FUN("hello")
Run Code Online (Sandbox Code Playgroud)
输出:
hello 1
hello 2
hello 3
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
Run Code Online (Sandbox Code Playgroud)
期望的输出:
hello 1
hello 2
hello 3
Run Code Online (Sandbox Code Playgroud) 我在这里问了一个相关的问题并且响应运行良好: 使用parallel的parLapply:无法访问并行代码中的变量
问题是,当我尝试使用函数内部的答案时,它将无法工作,因为我认为它具有默认环境clusterExport.我已经阅读了小插图并查看了帮助文件,但我的知识库非常有限.我使用的方式我parLapply期望它的行为类似lapply但似乎没有.
这是我的尝试:
par.test <- function(text.var, gc.rate=10){
ntv <- length(text.var)
require(parallel)
pos <- function(i) {
paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
}
cl <- makeCluster(mc <- getOption("cl.cores", 4))
clusterExport(cl=cl, varlist=c("text.var", "ntv", "gc.rate", "pos"))
parLapply(cl, seq_len(ntv), function(i) {
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
)
}
par.test(rep("I like cake and ice cream so much!", 20))
#gives this error message
> par.test(rep("I like cake and ice cream so much!", 20))
Error in …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用knitr和pandoc转换为PDF时控制绘图的位置.我的.Rmd文件看起来像这样:
# My report
Some text some text some text some text some text some text some text some text some text
```{r myplot, echo=FALSE, fig.pos="placeHere", results='hide'}
library(ggplot2)
ggplot(mtcars, aes(mpg, drat)) + geom_point()
```
Some text some text some text some text some text some text some text some text some text
\usepackage{graphicx}
\begin{figure}[placeHere]
\centering
\includegraphics[width=0.5\textwidth]{placeHere}
\end{figure}
Some text some text some text some text some text some text some text some text some text
Run Code Online (Sandbox Code Playgroud)
我正在使用此处提供的功能转换为PDF:http://quantifyingmemory.blogspot.co.uk/2013/02/reproducible-research-with-r-knitr.html
如何在第二和第三块文本之间放置图?乳胶代码目前无法正常工作. …