我有一个带有列的数据框,x1, x2, group
我想生成一个新的数据框,其中有一个额外的列rank
,表示x1
其组中的顺序.
还有一个相关的问题在这里,但接受的答案似乎不工作了.
直到这里,没关系:
library(dplyr)
data(iris)
by_species <- iris %>%
arrange(Species, Sepal.Length) %>%
group_by(Species)
Run Code Online (Sandbox Code Playgroud)
但是当我试图按组排名时:
by_species <- mutate(by_species, rank=row_number())
Run Code Online (Sandbox Code Playgroud)
错误是:
排名错误(x,ties.method ="first",na.last ="keep"):
缺少参数"x",没有默认值
更新
问题是dplyr
和之间存在一些冲突plyr
.要重现错误,请加载两个包:
library(dplyr)
library(plyr)
data(iris)
by_species <- iris %>%
arrange(Species, Sepal.Length) %>%
group_by(Species) %>%
mutate(rank=row_number())
# Error in rank(x, ties.method = "first", na.last = "keep") :
# argument "x" is missing, with no default
Run Code Online (Sandbox Code Playgroud)
卸载plyr
它的工作原理:
detach("package:plyr", unload=TRUE)
by_species <- iris %>% …
Run Code Online (Sandbox Code Playgroud) 给出两个简单的图:
library(igraph)
g <- graph.empty()
g <- g + vertices(1,2,3)
g <- g + path(1,2,3)
g1 <- g
V(g1)$color = c(1,2,2)
g2 <- g
V(g2)$color = c(2,1,1)
Run Code Online (Sandbox Code Playgroud)
看起来像:
par(mfrow=c(1,2))
palette(rainbow(3))
plot(g1)
plot(g2)
Run Code Online (Sandbox Code Playgroud)
他们为什么不同构?
graph.isomorphic.vf2(g1,g2)$iso
Run Code Online (Sandbox Code Playgroud)
假
最重要的是,如果这不是同构,我怎样才能在内部检测到这种等价igraph
?
在ipython笔记本中工作时,最终我不得不按Ctrl + C,因为内核似乎已停止.
控制台给了我一条消息:
[NotebookApp] Kernel shutdown: 5faa86bf-........f6
[NotebookApp] Kernel shutdown: 71........22
[NotebookApp] ....
Run Code Online (Sandbox Code Playgroud)
(我有三台笔记本电脑正在运行)
但出了点问题,我的笔记本文件.ipinb是空的(实际上只有我正在使用的那个).
有没有办法在删除之前恢复该文件?存储自动保存的手动保存版本的地方?
(在Windows 7中运行python 2.7(Anaconda))
给定一组用C颜色着色的N个元素,如何找到包含不超过最多M种颜色的长度L的每种可能组合?
我尝试使用itertools.combinations生成所有可能组合的算法,然后筛选出那些不具有最大颜色condiction的组合.
from itertools import combinations as cb
def allowed_combinations(elements, combination_size=4, max_colors=3):
colors = set([c for k, c in elements.items()])
combinations = cb(elements, combination_size)
for combination in combinations:
colors = set([elements[element] for element in combination])
if len(colors) > max_colors:
continue
yield combination
elements = dict()
elements['A'] = 'red'
elements['B'] = 'red'
elements['C'] = 'blue'
elements['D'] = 'blue'
elements['E'] = 'green'
elements['F'] = 'green'
elements['G'] = 'green'
elements['H'] = 'yellow'
elements['I'] = 'white'
elements['J'] = 'white'
elements['K'] = 'black'
combinations = …
Run Code Online (Sandbox Code Playgroud) 我想答案是32位,但我有点困惑为什么我甚至可以在win32中安装Anaconda 64.
我曾经在Anaconda 64位上工作,但我刚刚意识到我的系统是win32,这会不时产生一些例外.例如,请参阅我为scipy打开的这个问题:
https://github.com/scipy/scipy/issues/4524
根据我的系统信息,我有一个64位操作系统.所以:
(我不知道为什么我在"64位操作系统"上有win32)
当我开始一个python会话时,它说:
win32上的Anaconda 2.1; 0(64位)(默认; 2014年7月2日)[MSC v.1 500 64位(AMD64)].
在为位置分配值时,我发现了这种奇怪的行为.如果变量与列相同,则认为我们正在讨论该列:
library(data.table)
dt1 <- data.table(a = integer(1))
a <- 18
dt1[1, a:=a]
Run Code Online (Sandbox Code Playgroud)
结果:
Run Code Online (Sandbox Code Playgroud)> dt1 a 1: 0
我们可以使用不同的名称来避免这种情况
dt2 <- data.table(a = integer(1))
b <- 18
dt2[1, a:=b]
Run Code Online (Sandbox Code Playgroud)
结果:
Run Code Online (Sandbox Code Playgroud)>dt2 a 1: 18
但是,有没有另一种方法来做到这一点而不改变变量的名称?我读了关于.() ..()
符号但是我不确定我是否可以在这里使用它,例如:
dt1 <- data.table(a = integer(1))
a <- 18
dt1[1, a:=..(a)]
Error in eval(expr, envir, enclos) : could not find function ".."
Run Code Online (Sandbox Code Playgroud) 我想用ggplot2绘制一个像这样的混淆矩阵:
# Original data
samples <- t(rmultinom(50, size = 7, prob = rep(0.1,10)))
# Co-ocurrence matrix
coincidences <- sapply(1:ncol(samples), function(i){ colSums(samples[,i]==samples) })
Run Code Online (Sandbox Code Playgroud)
如果我使用geom_roster:
p <- ggplot(melt(coincidences), aes(Var1,Var2, fill=value)) + geom_raster()
Run Code Online (Sandbox Code Playgroud)
(这是问题Icons as x-axis labels in R的扩展。它寻找ggplot解决方案而不是绘图解决方案。由于ggplot
基于grid并且plot
基于graphics,方法非常不同)
我想绘制这样的图(来自本文),其中图标(在本例中为小图形)用作刻度标签。
原始问题的公认答案是:
library(igraph)
npoints <- 15
y <- rexp(npoints)
x <- seq(npoints)
# reserve some extra space on bottom margin (outer margin)
par(oma=c(3,0,0,0))
plot(y, xlab=NA, xaxt='n', pch=15, cex=2, col="red")
lines(y, col='red', lwd=2)
# graph numbers
x = 1:npoints
# add offset to first graph for centering
x[1] = x[1] + 0.4
x1 = grconvertX(x=x-0.4, from = 'user', to …
Run Code Online (Sandbox Code Playgroud) 我有一组文件名,如:
filelist <- c("filea-10.txt", "fileb-2.txt", "filec-1.txt", "filed-5.txt", "filef-4.txt")
Run Code Online (Sandbox Code Playgroud)
我想根据" - "之后的数字过滤它们.
例如,在python中,我可以使用key
排序函数的参数:
filelist <- ["filea-10.txt", "fileb-2.txt", "filec-1.txt", "filed-5.txt", "filef-4.txt"]
sorted(filelist, key=lambda(x): int(x.split("-")[1].split(".")[0]))
> ["filec-1.txt", "fileb-2.txt", "filef-4.txt", "filed-5.txt", "filea-10.txt"]
Run Code Online (Sandbox Code Playgroud)
在R,我有玩strsplit
,并lapply
没有运气这么远.
在R中这样做的方法是什么?
编辑:文件名可以是很多东西,可能包含更多数字.唯一固定的模式是我想要排序的数字是在" - "之后.另一个(真实的)例子:
c <- ("boards10017-51.mp4", "boards10065-66.mp4", "boards10071-81.mp4",
"boards10185-91.mp4", "boards10212-63.mp4", "boards1025-51.mp4",
"boards1026-71.mp4", "boards10309-89.mp4", "boards10310-68.mp4",
"boards10384-50.mp4", "boards10398-77.mp4", "boards10419-119.mp4",
"boards10421-85.mp4", "boards10444-87.mp4", "boards10451-60.mp4",
"boards10461-81.mp4", "boards10463-52.mp4", "boards10538-83.mp4",
"boards10575-62.mp4", "boards10577-249.mp4")"
Run Code Online (Sandbox Code Playgroud) 对于我正在实现的聚类算法,我想随机初始化聚类分配.但是,我需要没有差距.也就是说,这不行:
set.seed(2)
K <- 10 # initial number of clusters
N <- 20 # number of data points
z_init <- sample(K,N, replace=TRUE) # initial assignments
z_init
# [1] 2 8 6 2 10 10 2 9 5 6 6 3 8 2 5 9 10 3 5 1
sort(unique(z_init))
# [1] 1 2 3 5 6 8 9 10
Run Code Online (Sandbox Code Playgroud)
标签4和7未使用的地方.
相反,我希望这个向量是:
# [1] 2 6 5 2 8 8 2 7 4 5 5 3 6 2 4 7 8 …
Run Code Online (Sandbox Code Playgroud) r ×7
ggplot2 ×2
python ×2
algorithm ×1
anaconda ×1
combinations ×1
data.table ×1
dplyr ×1
graph ×1
igraph ×1
ipython ×1
isomorphism ×1
regex ×1
sorting ×1
winapi ×1