小编all*_*nvc的帖子

在Rcpp中排序排列,即base :: order()

我有很多代码使用base :: order()命令,我真的懒得在rcpp中编写代码.由于Rcpp只支持排序,但不支持订单,我花了2分钟创建这个功能:

// [[Rcpp::export]]
Rcpp::NumericVector order_cpp(Rcpp::NumericVector invec){
  int leng = invec.size();
  NumericVector y = clone(invec);
  for(int i=0; i<leng; ++i){
    y[sum(invec<invec[i])] = i+1;
  }
  return(y);
}
Run Code Online (Sandbox Code Playgroud)

它有点工作.如果向量包含唯一数字,我得到与order()相同的结果.如果它们不是唯一的,结果是不同的,但没有错(实际上没有唯一的解决方案).

使用它:

c=sample(1:1000,500)
all.equal(order(c),order_cpp(c))
microbenchmark(order(c),order_cpp(c))

Unit: microseconds
         expr      min       lq   median       uq      max neval
     order(c)   33.507   36.223   38.035   41.356   78.785   100
 order_cpp(c) 2372.889 2427.071 2466.312 2501.932 2746.586   100
Run Code Online (Sandbox Code Playgroud)

哎哟! 我需要一个有效的算法.好的,所以我挖出了一个bubbleort实现并对其进行了调整:

 // [[Rcpp::export]]
Rcpp::NumericVector bubble_order_cpp2(Rcpp::NumericVector vec){                                  
       double tmp = 0;
       int n = vec.size();
              Rcpp::NumericVector outvec = clone(vec);
       for (int i …
Run Code Online (Sandbox Code Playgroud)

r rcpp

7
推荐指数
2
解决办法
2380
查看次数

在模式发生后拆分字符串

我有以下字符串向量.它包含两个元素.每个元素由两个折叠短语组成.

strings <- c("This is a phrase with a NameThis is another phrase",
         "This is a phrase with the number 2019This is another phrase")
Run Code Online (Sandbox Code Playgroud)

我想将这些短语拆分为向量中的每个元素.我一直在尝试这样的事情:

library(stringr)

str_split(strings, "\\B(?=[a-z|0-9][A-Z])")
Run Code Online (Sandbox Code Playgroud)

几乎给了我正在寻找的东西:

[[1]]
[1] "This is a phrase with a Nam" "eThis is another phrase"

[[2]]
[1] "This is a phrase with the number 201" "9This is another phrase"
Run Code Online (Sandbox Code Playgroud)

我想在模式之后进行拆分,但无法弄清楚如何做到这一点.

我想我接近一个解决方案,并希望得到任何帮助.

regex r stringr

6
推荐指数
1
解决办法
81
查看次数

闪亮的server.R无法上传库

我现在正尝试解决一个令人惊讶的错误。这是我的server.R的标头:

library(shiny)
library(plotly)
library(DT)
library(Rcpp)
library(RSQLite)
library(org.Mm.eg.db)
library(shinyBS)
library(igraph)
library(reshape2)
library(ggplot2)
library(org.Hs.eg.db)
library(visNetwork)
Run Code Online (Sandbox Code Playgroud)

我希望在运行应用程序时可以上传这些库,但是在3种情况下:visNetowrk,plotly和ShinyBS,我遇到了几个错误:

Error : could not find function "visNetworkOutput"
Error : could not find function "plotlyOutput"
Error : could not find function "bsTooltip"
Run Code Online (Sandbox Code Playgroud)

因此,我需要使用控制台手动包含这些库:

library(plotly); library(visNetwork); library(shinyBS)
Run Code Online (Sandbox Code Playgroud)

如何克服呢?提前致谢!

r shiny

5
推荐指数
1
解决办法
2639
查看次数

如何使用separate()函数分离"m014"

我试图将"new_sp_m014"分成四列(new,var,sex,age).我使用了以下块:

task2 %>% separate(code, into = c("new","var","sex","age"), sep = "_")
Run Code Online (Sandbox Code Playgroud)

并且结果适用于new ="new",var ="sp",但是sex ="m014",年龄是NA.

那我怎么能把"m014"分为sex ="m"和age ="014"呢?

r

2
推荐指数
1
解决办法
73
查看次数

如何使用Rcpp找到矩阵的两个特定列之间的最小值的索引?

我有一个 5x5 矩阵,想找到列“1”和“3”之间最小值的索引。在R我会这样做:

set.seed(1984)
m <- matrix(sample.int(25,25), 5)
min <- which(m[,c(1,3)] == min(m[,c(1,3)]), arr.ind = TRUE)
Run Code Online (Sandbox Code Playgroud)

使用 Rcpp 执行此操作的最有效方法是什么?

c++ r rcpp

-2
推荐指数
1
解决办法
501
查看次数

标签 统计

r ×5

rcpp ×2

c++ ×1

regex ×1

shiny ×1

stringr ×1