我正在使用Anaconda python发行版,并希望使用selenium包.不幸的是,发行版中没有包含硒,所以我使用推荐的安装:
pip install -U selenium
Run Code Online (Sandbox Code Playgroud)
分布常见问题解答说,这应该能正常运行,但是当我尝试使用它,我得到蟒蛇告诉我,它不知道这包东西.
即
>>> import selenium
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named selenium
Run Code Online (Sandbox Code Playgroud)
我查了一下,/usr/local/lib/python2.7/dist-packages目录中确实有selenium.我如何指向我的python发行版,以便我可以使用该包?
我有数据,其中每个观察的ID是作为序列存储的数字,通常以X:Y的形式,但有时是连接列表.我想整理数据,以便每个观察都有自己的行,以便我可以使用连接函数添加更多描述性ID.通常情况下,我会使用gather()函数tidyr来执行此操作,但我无法解压缩ID,因为它们是字符.
数据如下所示:
example <- data_frame(x = LETTERS[1:3], y = c("Condition 1", "Condition 2", "Condition 3"), z = c("1:3", "4:6", "c(7,9,10)"))
example
# A tibble: 3 × 3
x y z
<chr> <chr> <chr>
1 A Condition 1 1:3
2 B Condition 2 4:6
3 C Condition 3 c(7,9,10)
Run Code Online (Sandbox Code Playgroud)
然而,这些不起作用,并产生NA:
as.numeric("1:3")
as.integer("1:3")
as.numeric("c(7,9,10)")
as.integer("c(7,9,10)")
Run Code Online (Sandbox Code Playgroud)
必须有一个简单的方法来做到这一点,但我想一个很长的路可能是提取数字并首先将它们存储为列表.对于X:Y ID,我可以通过将字符串拆分为":",然后从一个数字到另一个数字创建一个序列,如下所示:
example[1:2,] %>%
+ separate(z, c("a", "b"), sep = ":") %>%
+ mutate(a = as.numeric(a), b = as.numeric(b), new = …Run Code Online (Sandbox Code Playgroud) 使用此示例数据:
library(tidyverse)
set.seed(123)
df <- data_frame(X1 = rep(LETTERS[1:4], 6),
X2 = sort(rep(1:6, 4)),
ref = sample(1:50, 24),
sampl1 = sample(1:50, 24),
var2 = sample(1:50, 24),
meas3 = sample(1:50, 24))
Run Code Online (Sandbox Code Playgroud)
我可以summarise_at()用来计算列子集中的值的数量:
df %>% summarise_at(vars(contains("2")), funs(sd_expr = n() ))
Run Code Online (Sandbox Code Playgroud)
这不是很令人兴奋,因为它与行数相同.但是,它在具有嵌套列的表中很有用,每个单元格包含一个数据帧,每个单元格中的行数不同.
例如,
df %>%
mutate_at(vars(-one_of(c("X1", "X2", "ref"))), funs(first = . - ref)) %>%
mutate_at(vars(contains("first")), funs(second = . *2 )) %>%
nest(-X1) %>%
mutate(mean = map(data,
~ summarise_at(.x, vars(contains("second")),
funs(mean_second = mean(.) ))),
n = map(data,
~ summarise_at(.x, vars(contains("second")),
funs(n_second = n() …Run Code Online (Sandbox Code Playgroud) 使用ggplot2绘制计数geom_bar(stat="identity")是一种可视化计数的有效方法。我想使用此方法来显示观察到的计数,并将它们与期望计数进行比较,我希望通过使用geom_step在阶梯图上覆盖阶梯图绘图层的方式来实现此目的。
但是,当我这样做时,我遇到了一个问题,即默认情况下,条形图的位置是闪避的,但geom_step不是。例如,同时使用连续和离散因变量:
library(tidyverse)
test <- data_frame(a = 1:10, b = runif(10, 1, 10))
test_plot <- ggplot(test, aes(a, b)) +
geom_bar(stat="identity") +
geom_step(color = 'red')
test2 <- data_frame(a = letters[1:10], b = runif(10, 1, 10))
test2_plot <- ggplot(test2, aes(a, b, group = 1)) +
geom_bar(stat="identity") +
geom_step(color = 'red'))
gridExtra::grid.arrange(test_plot, test2_plot, ncol = 2)
Run Code Online (Sandbox Code Playgroud)
如您所见,这两层是偏移的,这是不希望的。
阅读文档后,我看到geom_path有一个position =选择,但是尝试类似的操作geom_step(color = 'red', position = position_dodge(width = 0.5))并没有达到我想要的效果,而是将钢筋和阶梯线压缩到中心。另一个选择是像这样直接调整数据,这geom_step(aes(a-0.5, b), color = 'red') …
我有这样的数据:
library(dplyr)
set.seed(123)
df <- data_frame(X1 = rep(LETTERS[1:4], 6),
X2 = rep(1:2, 12),
ref = sample(1:50, 24),
sampl1 = sample(1:50, 24),
var2 = sample(1:50, 24),
meas3 = sample(1:50, 24))
Run Code Online (Sandbox Code Playgroud)
使用dplyrscooped命令,我可以一次编辑和创建多个列,例如:
df %>% mutate_if(is.numeric, funs(new = . - ref))
Run Code Online (Sandbox Code Playgroud)
如果我想只对列的一个子集执行此操作,我可以select像这样使用帮助程序:
df %>% mutate_at(vars(one_of(c("X2", "ref"))), funs(new = . - ref))
Run Code Online (Sandbox Code Playgroud)
然而,在我来说,我知道我的数据总是包含列X1,X2而ref但是想子集以这样的方式发生变异仅在非列中的数据X1,X2和ref.这些其他列的编号和名称将是可变的,但始终为数字.我以为我可以这样做:
df %>% mutate_at(vars(!one_of(c("X1", "X2", "ref"))), funs(new = . - ref))
Run Code Online (Sandbox Code Playgroud)
或者可能
df %>% mutate_at(vars(one_of(!names %in% …Run Code Online (Sandbox Code Playgroud) 我正在试验dplyr,tidyr和purrr。我有这样的数据:
library(tidyverse)
set.seed(123)
df <- data_frame(X1 = rep(LETTERS[1:4], 6),
X2 = sort(rep(1:6, 4)),
ref = sample(1:50, 24),
sampl1 = sample(1:50, 24),
var2 = sample(1:50, 24),
meas3 = sample(1:50, 24))
Run Code Online (Sandbox Code Playgroud)
现在dplyr很棒,因为我可以mutate_at()同时操作多个列。例如:
df <- df %>%
mutate_at(vars(-one_of(c("X1", "X2", "ref"))), funs(first = . - ref)) %>%
mutate_at(vars(contains("first")), funs(second = . *2 ))
Run Code Online (Sandbox Code Playgroud)
并tidyr允许我将数据的子集嵌套为单个列中的子表:
df <- df %>% nest(-X1)
Run Code Online (Sandbox Code Playgroud)
多亏了purrr我可以总结这些子表,同时保留嵌套列中的原始数据:
df %>% mutate(mean = map_dbl(data, ~ mean(.x$meas3_first_second)))
Run Code Online (Sandbox Code Playgroud)
如何使用purrr和 …
我遇到了这个问题,它有一个非常酷的图表。我对左侧的图形感兴趣,其中包含跨时间的矩形点。
这些矩形点不是 R 提供 geom_point() 命令的默认点集的一部分。虽然我可以重现图表(或至少一个非常相似的图表),但我不知道如何让这些点看起来像那样。
我怎样才能做到这一点?
我正在检测圆形物体的边缘,并获得"凹凸不平"的不规则边缘.是否有平滑的边缘,以便我有一个更均匀的形状?
例如,在下面的代码中,我生成一个"颠簸"的圆圈(左).我是否可以使用平滑或移动平均函数来获得或近似"平滑"圆(右).最好有一些参数,我可以控制,因为我的实际图像不是完美的圆形.
import numpy as np
import matplotlib.pyplot as plt
fig, (bumpy, smooth) = plt.subplots(ncols=2, figsize=(14, 7))
an = np.linspace(0, 2 * np.pi, 100)
bumpy.plot(3 * np.cos(an) + np.random.normal(0,.03,100), 3 * np.sin(an) + np.random.normal(0,.03,100))
smooth.plot(3 * np.cos(an), 3 * np.sin(an))
Run Code Online (Sandbox Code Playgroud)
我正在自学R tidyverse purr()程序包,并且map()在一列嵌套数据帧上实现时遇到了麻烦。有人可以解释我所缺少的吗?
以基本的R ChickWeight数据集为例,如果我像这样首先过滤饮食#1,就可以轻松获取饮食#1下每个时间点的观察次数:
library(tidyverse)
ChickWeight %>%
filter(Diet == 1) %>%
group_by(Time) %>%
summarise(counts = n_distinct(Chick))
Run Code Online (Sandbox Code Playgroud)
很好,但我想一次为每种饮食做一次,我认为嵌套数据并对其进行迭代map()将是一个好方法。这是我所做的:
example <- ChickWeight %>%
nest(-Diet)
Run Code Online (Sandbox Code Playgroud)
然后,实现此地图功能即可达到我的目标:
map(example$data, ~ .x %>% group_by(Time) %>% summarise(counts = n_distinct(Chick)))
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用管道将同一命令放入原始数据帧的另一列中来实现该命令时,它将失败。
example %>%
mutate(counts = map(data, ~ .x %>% group_by(Time) %>% summarise(counts = n_distinct(Chick))))
Error in eval(substitute(expr), envir, enclos) :
variable 'Chick' not found
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况?
我还尝试了将数据框拆分为一个列表,但没有成功。
ChickWeight %>%
split(.$Diet) %>%
map(data, ~ .x %>% group_by(Time) %>% summarise(counts = n_distinct(Chick)))
Run Code Online (Sandbox Code Playgroud)