R語(yǔ)言which函數(shù)介紹及Rcpp改寫(xiě)詳解
引言
首先來(lái)介紹一下R語(yǔ)言which函數(shù)的作用:which函數(shù)在向量、矩陣、數(shù)據(jù)框,列表、因子這些數(shù)據(jù)結(jié)構(gòu)中有這重要的作用,可以查找特定的元素返回其在數(shù)據(jù)中的索引,因此非常方便操作數(shù)據(jù)。
which 函數(shù)的介紹
which函數(shù)中的參數(shù):
function (x, arr.ind = FALSE, useNames = TRUE)
which函數(shù)的源碼:
which <- function(x, arr.ind = FALSE, useNames = TRUE)
{
wh <- .Internal(which(x))
if (arr.ind && !is.null(d <- dim(x)))
arrayInd(wh, d, dimnames(x), useNames=useNames) else wh
}
arrayInd <- function(ind, .dim, .dimnames = NULL, useNames = FALSE) {
##-- return a matrix length(ind) x rank == length(ind) x length(.dim)
m <- length(ind)
rank <- length(.dim)
wh1 <- ind - 1L
ind <- 1L + wh1 %% .dim[1L]
dnms <- if(useNames) {
list(.dimnames[[1L]][ind],
if(any(nzchar(nd <- names(.dimnames)))) nd else
if(rank == 2L) c(“row”, “col”) # for matrices
else paste0(“dim”, seq_len(rank)))
}
ind <- matrix(ind, nrow = m, ncol = rank, dimnames = dnms)
if(rank >= 2L) {
denom <- 1L
for (i in 2L:rank) {
denom <- denom * .dim[i-1L]
nextd1 <- wh1 %/% denom # (next dim of elements) - 1
ind[,i] <- 1L + nextd1 %% .dim[i]
}
}
storage.mode(ind) <- “integer”
ind
}供給想改進(jìn)的同學(xué)學(xué)習(xí)!
which函數(shù)的一些小例子
1
x <- sample(1:10,25,T)
x的值: 10 9 3 10 9 9 10 10 3 10 7 9 9 7 2 4 2 8 8 5 4 7 3 8 4
which(x == 10)
10在向量x中的位置:1 4 7 8 10
給向量x命名,測(cè)試一下useNames = TRUE是否起作用!
names(x) <- letters[1:25] which(x == 10,useNames = FALSE) which(x == 10,useNames = TRUE)

然而并沒(méi)有什么卵用!
2
a <- matrix(rep(1:3,times = c(3,3,3)),3,3) which(a == 1,arr.ind = T) which(a == 1,arr.ind = F) which(a == 1,arr.ind = T,useNames = TRUE) which(a == 1,arr.ind = F,useNames = FALSE)
結(jié)果:

which函數(shù)的改進(jìn)以及時(shí)間對(duì)比
針對(duì)向量版本的我這里就不展示了!
根據(jù)矩陣中的某個(gè)元素返回其在矩陣中的位置!
Rcpp代碼:
sourceCpp(code = '
#include <RcppArmadillo.h>
//[[Rcpp::depends("RcppArmadillo")]]
//[[Rcpp::export]]
arma::mat whicha(arma::mat matrix,int what){
arma::uvec out;//查找索引值
out = find(matrix == what);//查找索引值(從0開(kāi)始)
int n = matrix.n_rows; //行數(shù)
int nl = out.n_elem; //查找元素總數(shù)
arma::vec foo;
arma::mat out1(nl,2); //輸出矩陣
foo = arma::conv_to<arma::vec>::from(out); //查找值所在的向量索引
out1.col(1) = floor(foo / n)+1;
for(int i = 0;i < nl;i++){
out1(i,0) = floor(out(i) % n)+1;}
return out1; } ')
時(shí)間對(duì)比:
library(microbenchmark)
microbenchmark(which(z == 1,arr.ind = T),
whicha(z,1) )

總結(jié)
到此這篇關(guān)于R語(yǔ)言which函數(shù)介紹及Rcpp改寫(xiě)的文章就介紹到這了,更多相關(guān)R語(yǔ)言which函數(shù)詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
R包c(diǎn)lusterProfiler如何安裝成功(新手必看!)
最近在我以為ClusterProfiler已經(jīng)安裝好的時(shí)候,又遇到了一些問(wèn)題,所以這篇文章主要給大家介紹了關(guān)于R包c(diǎn)lusterProfiler如何安裝成功的相關(guān)資料,需要的朋友可以參考下2023-02-02
R語(yǔ)言學(xué)習(xí)之字符串和時(shí)間格式化詳解
這篇文章主要為大家詳細(xì)介紹了R語(yǔ)言中字符串和時(shí)間格式化的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
R語(yǔ)言學(xué)習(xí)筆記之lm函數(shù)詳解
這篇文章主要介紹了R語(yǔ)言學(xué)習(xí)筆記之lm函數(shù)詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
R語(yǔ)言使用ggplot繪制畫(huà)中畫(huà)細(xì)節(jié)放大的方法
這篇文章主要為大家介紹了R語(yǔ)言使用ggplot繪制畫(huà)中畫(huà)細(xì)節(jié)放大的方法實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11

