在Python中,您可以使用原始字符串:
import re
re.sub(r"\\", ":", "back\\slash") # r"\\" instead of "\\\\"
Run Code Online (Sandbox Code Playgroud)
R中也存在吗?例如,这是一个等效的代码片段,其中R中没有原始字符串:
library(stringr)
str_replace("back\\slash", "\\\\", ":")
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这一点:
str_replace("back\\slash", raw("\\"), ":")
Run Code Online (Sandbox Code Playgroud)
此功能是否已经存在,还是应该只实现自己的功能raw()?
为什么以下情节看起来不同?两种方法似乎都使用高斯内核.
如何ggplot2计算密度?
library(fueleconomy)
d <- density(vehicles$cty, n=2000)
ggplot(NULL, aes(x=d$x, y=d$y)) + geom_line() + scale_x_log10()
Run Code Online (Sandbox Code Playgroud)
ggplot(vehicles, aes(x=cty)) + geom_density() + scale_x_log10()
Run Code Online (Sandbox Code Playgroud)
更新:
这个问题的解决方案已经在SO上出现了,但是ggplot2传递给R stats密度函数的具体参数仍然不清楚.
一种替代的解决方案是从GGPLOT2情节直提取密度数据,如图这里
我有一个在一个单独的线程中初始化的对象.在填充本地数据库时,初始化可能需要几秒钟.
SpecialAnalysis currentAnalysis = new SpecialAnalysis(params_here);
Run Code Online (Sandbox Code Playgroud)
我正在尝试实现一个"取消"按钮,它将对象的isCancelled布尔值设置为true.实现这个的正确Java方法是什么?
while (currentAnalysis == null) {
}
currentAnalysis.cancel();
Run Code Online (Sandbox Code Playgroud)
该方法冻结了程序,因为它似乎进入了计算效率低下的循环.这是我可以使用的情况Object.wait()吗?
我目前的糟糕/半成功的解决方案是:
while (currentAnalysis == null) {
Thread.sleep(500);
}
currentAnalysis.cancel();
Run Code Online (Sandbox Code Playgroud)
谢谢!
鉴于以下列表:
l <- list("foo123"=c(1:3), "foo456"=5, "foo789"=8)
print(l)
# $foo123
# [1] 1 2 3
#
# $foo456
# [1] 5
#
# $foo789
# [1]
Run Code Online (Sandbox Code Playgroud)
当我unlist()在列表中时,如果名称是重复的,则名称会附加整数.
unlist(l)
# foo1231 foo1232 foo1233 foo456 foo789
# 1 2 3 5 8
Run Code Online (Sandbox Code Playgroud)
我想保留名字,所以use.names=FALSE不理想.是否在帮助页面的任何位置解释了此行为?可以修改吗?
可以将unlist配置为保留名称,以便我的结果是:
# foo123 foo123 foo123 foo456 foo789
# 1 2 3 5 8
Run Code Online (Sandbox Code Playgroud) 我想使用 来创建一个 gif gganimate,但我的轴范围在一帧中变化很大。这导致所有后续帧都被压缩。
在ggplot2某些方面,有一个选项可以让scales="free". 有没有办法在 的每一帧中都有自由比例gganimate?
下面是一个例子:
library(gapminder)
library(ggplot2)
library(gganimate)
theme_set(theme_bw())
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent,
frame = year)) +
geom_point() +
scale_x_log10()
gganimate(p)
Run Code Online (Sandbox Code Playgroud)
现在我们将其中一个数据点移动到某个极值。这会挤压所有后续未受影响帧中的点。
gapminder[1, "lifeExp"] <- 1000
gapminder[1, "gdpPercap"] <- 1e60
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent,
frame = year)) +
geom_point() +
scale_x_log10()
gganimate(p) # smooshed
Run Code Online (Sandbox Code Playgroud)
下面我构建了一个Upset图.我正在使用颜色调色板来定义条形颜色.有没有办法为连接点的矩阵做到这一点?
library(dplyr)
library(RColorBrewer)
library(UpSetR)
movies <- read.csv(system.file("extdata", "movies.csv",
package = "UpSetR"), header=T, sep=";" )
movies <- select(movies, Action:Children)
upset(movies, main.bar.color=brewer.pal(2^ncol(movies)-1, "Set1"))
Run Code Online (Sandbox Code Playgroud)
当尝试将调色板应用于矩阵时,我收到警告,并且仅使用第一种颜色红色.
upset(movies, main.bar.color=brewer.pal(2^ncol(movies)-1, "Set1"),
matrix.color=brewer.pal(2^ncol(movies)-1, "Set1"))
Run Code Online (Sandbox Code Playgroud)
在OS X Mavericks上使用Python3时,我遇到了分段错误.有关如何解决此问题的任何建议?
我试图从Python站点重新安装包,但这是无效的.如何在系统上重新编译Python3?
我正在使用parallel包在R中执行并行化代码mclapply,并将预定义数量的内核作为参数.
如果我有一份将要运行几天的工作,我是否有办法编写(或换行)我的mclapply功能,以便在服务器高峰时段使用更少的内核,并在非高峰时段提高使用率?
给定除最后一列之外的所有列中都包含数值的数据框,如何计算该行的平均值?
在这个例子中,我使用的是所有列,包括name我想省略的列.
df <- as.data.frame(matrix(1:40, ncol=10)) %>%
mutate(name=LETTERS[1:4]) %>%
mutate(mean=rowMeans(.))
Run Code Online (Sandbox Code Playgroud)
期望的数据帧输出:
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 mean name
1 1 5 9 13 17 21 25 29 33 37 19 A
2 2 6 10 14 18 22 26 30 34 38 20 B
3 3 7 11 15 19 23 27 31 35 39 21 C
4 4 8 12 16 20 24 28 32 36 40 22 D
Run Code Online (Sandbox Code Playgroud) 有没有办法在一行中进行以下更换R?如果可能的话,会更多/更低效吗?
m <- matrix(rnorm(100), ncol=10)
threshold <- 0.5
# Is there a single-line way to do the following in R
m[m < threshold] <- 0
m[m >= threshold] <- 1
Run Code Online (Sandbox Code Playgroud)
我想知道ifelse()函数是否可以容纳这个,在if <threshold然后是0的意义上,否则为1
有谁知道python3使用什么样的哈希函数?有没有办法重新实现函数接受列表作为键?
r ×8
python-3.x ×2
axes ×1
density-plot ×1
dictionary ×1
dplyr ×1
gganimate ×1
ggplot2 ×1
java ×1
list ×1
matrix ×1
python ×1
regex ×1
upsetr ×1
wait ×1