whichDim: can determine the array-'position' of an element number

View source: R/tools.R

whichDimR Documentation

can determine the array-'position' of an element number

Description

can determine the array-'position' of an element number

Usage

whichDim(element, dimensions)

Arguments

element

integer coming usually coming from which(...). Note: can only be a single number

dimensions

dimensions of the (multidimensional) array from which the element comes

Value

array with position of the element

Note

based on multi.which by M vd Loo, 2011

https://www.r-bloggers.com/2011/09/a-multidimensional-which-function/

example code: set.seed(83) ar3 <- array(rbinom(20,1,0.5), dim = c(4,5)) ar3

ar3[1,1] <- 2 ar3[2,1] <- 2 ar3[1,2] <- 2 ar3[3,4] <- 2 ar3[2,5] <- 2 ar3[4,5] <- 2

ar3

which(ar3 == 2) multi.which(ar3 == 2) whichDim(20, dimensions = dim(ar3)) # to get positions of all elements that are 2 purrr::map_df(which(ar3 == 2), ~as.data.frame(whichDim(.x, dimensions = dim(ar3))))


BenBruyneel/BBPersonalR documentation built on Aug. 23, 2024, 8:28 p.m.