给出一个矢量:
labels <- c(1,2,3,3,3)
Run Code Online (Sandbox Code Playgroud)
如何获得所有可能的群组重新标记?对于这个例子:
1,2,3,3,3
1,3,2,2,2
2,1,3,3,3
2,3,1,1,1
3,1,2,2,2
3,2,1,1,1
Run Code Online (Sandbox Code Playgroud)
我一直在看permute包,但我不知道如何将它应用于这种情况.
我希望能够在我在Github托管的Jekyll网站中插入YouTube视频.
有一个用于嵌入youtube视频的Jekyll 插件,但它在github页面中不起作用.
iframe也不起作用.
<iframe width="420" src="hhtp://..." frameborder="0" allowfullscreen> </iframe>
Run Code Online (Sandbox Code Playgroud)有没有办法做到这一点,或者我们只需要忍受它?
在处理loglikelihoods时,我有时会想到:
alpha*log(x) # log version of log(x^alpha)
Run Code Online (Sandbox Code Playgroud)
在非对数的情况下,如果两个x 和alpha是零,R假定0 ^ 0 = 1,这是通常所期望的行为(例如:场景"零组的意见,即具有零概率"具有概率1).但是在alpha*log(x)版本中给出了NaN:
alpha <- 0
x <- 0
log(x^alpha) # gives 0
alpha*log(x) # gives NaN
Run Code Online (Sandbox Code Playgroud)
我读过这0*Inf = NaN是一个IEEE标准,但我们应该做什么呢?为alpha = 0创建特定案例?在这种情况下不使用日志?别的什么?
我想这是一个非常常见的场景,我想知道其他人是如何处理它的,或者是否有一些常见的做法.
我有一个在两个节点之间有许多边的图.当我绘制它时,我得到一个警告我在这种情况下不理解.
这很好用:
library(igraph)
gg <- graph.empty(n=0, directed=TRUE)
gg <- gg + vertex("10501")
gg <- gg + edge("10501", "10501")
gg <- gg + edge("10501", "10501")
plot(gg)
Run Code Online (Sandbox Code Playgroud)
但是,带有一个边缘的相同图形会发出警告:
gg <- graph.empty(n=0, directed=TRUE)
gg <- gg + vertex("10501")
gg <- gg + edge("10501", "10501")
gg <- gg + edge("10501", "10501")
gg <- gg + edge("10501", "10501")
plot(gg)
Run Code Online (Sandbox Code Playgroud)
我没有看到原因.为什么这个警告?
我想从推文中提取用户名,这些用户名可能是:
例如,从这里:
"RT@user1: This is a retweet that mentions @user2."
Run Code Online (Sandbox Code Playgroud)
我想得到一个像
[1] @user1 @user2
Run Code Online (Sandbox Code Playgroud)
(带或不带“@”)
这是我当前的脚本:
text <- "RT@user1: This is a retweet that mentions @user2."
tokens <- unlist(strsplit(text, " "))
mentions.mask <- grepl("@\\w+", tokens)
mentions <- tokens[mentions.mask]
cat(mentions)
[1] "RT@user1:" "@user2."
Run Code Online (Sandbox Code Playgroud)
我怎样才能正确地做到这一点?
我需要建立一个很大的data.table地方,每一行都是用户,而列是不同类型的属性。我需要逐行填写表格。我应该如何初始化它?
例如,如果我这样做:
dt.hetero <- data.table(matrix(-1, nrow=3, ncol=6))
names(dt.hetero) <- c("name", "lastname", "city", "age", "weight", "heigh")
dt.hetero[1, age:=34]
dt.hetero[1, name:="alice"]
Run Code Online (Sandbox Code Playgroud)
它期望在任何地方都翻倍,因此当我尝试输入字符串时收到警告:
Warning messages:
1: In `[.data.table`(dt.hetero, 1, `:=`(name, "alice")) :
NAs introduced by coercion
2: In `[.data.table`(dt.hetero, 1, `:=`(name, "alice")) :
Coerced 'character' RHS to 'double' to match the column's type. Either change the target column to 'character' first (by creating a new 'character' vector length 3 (nrows of entire table) and assign that; i.e. 'replace' column), or coerce …Run Code Online (Sandbox Code Playgroud) 我有一个4维数组,我需要计算其两个维度的总和.我看到这apply是非常缓慢的.
我试过这个compiler库,但速度几乎没有提高:
library(compiler)
X <- array(2, dim=c(1000,20,10,125))
suma <- function(X){
apply(X, MARGIN=c(1,2), sum)
}
suma.cmp <- cmpfun(suma)
benchmark(suma.cmp(X), suma(X), replications = 50)
# test replications elapsed relative user.self sys.self user.child
#1 suma.cmp(X) 50 24.616 1.000 24.164 0.424 0
#2 suma(X) 50 24.892 1.011 24.440 0.416 0
Run Code Online (Sandbox Code Playgroud)
我会试试Rcpp,但RcppArmadillo据我所知,没有4维数组.
如何apply(X, MARGIN=c(1,2), sum)尽可能快地进行此计算()?
r ×6
data.table ×1
github-pages ×1
grepl ×1
igraph ×1
jekyll ×1
permutation ×1
permute ×1
rcpp ×1
regex ×1
youtube ×1