網站首頁 編程語言 正文
引言
首先來介紹一下R語言which函數的作用:which函數在向量、矩陣、數據框,列表、因子這些數據結構中有這重要的作用,可以查找特定的元素返回其在數據中的索引,因此非常方便操作數據。
which 函數的介紹
which函數中的參數:
function (x, arr.ind = FALSE, useNames = TRUE)
which函數的源碼:
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
}
供給想改進的同學學習!
which函數的一些小例子
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命名,測試一下useNames = TRUE是否起作用!
names(x) <- letters[1:25]
which(x == 10,useNames = FALSE)
which(x == 10,useNames = TRUE)
然而并沒有什么卵用!
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)
結果:
which函數的改進以及時間對比
針對向量版本的我這里就不展示了!
根據矩陣中的某個元素返回其在矩陣中的位置!
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開始)
int n = matrix.n_rows; //行數
int nl = out.n_elem; //查找元素總數
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; } ')
時間對比:
library(microbenchmark)
microbenchmark(which(z == 1,arr.ind = T),
whicha(z,1) )
總結
原文鏈接:https://blog.csdn.net/weixin_43217641/article/details/123479906
相關推薦
- 2022-11-29 C#使用泛型隊列Queue實現生產消費模式_C#教程
- 2023-01-11 ubuntu如何搭建vsftpd服務器_FTP服務器
- 2023-08-01 el-date-picker組件中渲染多余文字
- 2022-12-31 React實現合成事件的源碼分析_React
- 2021-12-11 Linux環境下查看日志文件命令詳解_Linux
- 2022-09-15 python?Pandas庫read_excel()參數實例詳解_python
- 2023-05-14 Go結構體的基本使用詳解_Golang
- 2023-01-01 C語言之如何求三次方根_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支