Displaying 9 results from an estimated 9 matches for "subset_row".
2018 Jun 08
6
Subsetting the "ROW"s of an object
Hi all,
Is there a better to way to subset the ROWs (in the sense of NROW) of
an vector, matrix, data frame or array than this?
subset_ROW <- function(x, i) {
nd <- length(dim(x))
if (nd <= 1L) {
x[i]
} else {
dims <- rep(list(quote(expr = )), nd - 1L)
do.call(`[`, c(list(quote(x), quote(i)), dims, list(drop = FALSE)))
}
}
subset_ROW(1:10, 4:6)
#> [1] 4 5 6
str(subset_ROW(array(1:10, c(10)), 2:4))...
2018 Jun 08
2
Subsetting the "ROW"s of an object
...kham at gmail.com> wrote:
>>
>> Hi all,
>>
>> Is there a better to way to subset the ROWs (in the sense of NROW) of
>> an vector, matrix, data frame or array than this?
>
>
> You can use TRUE to fill the subscripts for dimensions 2:nd
>
>>
>> subset_ROW <- function(x, i) {
>> nd <- length(dim(x))
>> if (nd <= 1L) {
>> x[i]
>> } else {
>> dims <- rep(list(quote(expr = )), nd - 1L)
>> do.call(`[`, c(list(quote(x), quote(i)), dims, list(drop = FALSE)))
>> }
>> }
>
>
> s...
2018 Jun 08
1
Subsetting the "ROW"s of an object
On 06/08/2018 10:15 AM, Michael Lawrence wrote:
> There probably should be an abstraction for this. In S4Vectors, we
> have extractROWS().
FWIW the code in S4Vectors that does what your subset_ROW() does is:
https://github.com/Bioconductor/S4Vectors/blob/04cc9516af986b30445e99fd1337f13321b7b4f6/R/subsetting-utils.R#L466-L476
(This is the default "extractROWS" method.)
Except for the normalization of 'i', it does the same as your
subset_ROW(). I don't know how to do...
2018 Jun 08
0
Subsetting the "ROW"s of an object
...t;>> Hi all,
>>>
>>> Is there a better to way to subset the ROWs (in the sense of NROW) of
>>> an vector, matrix, data frame or array than this?
>>
>>
>> You can use TRUE to fill the subscripts for dimensions 2:nd
>>
>>>
>>> subset_ROW <- function(x, i) {
>>> nd <- length(dim(x))
>>> if (nd <= 1L) {
>>> x[i]
>>> } else {
>>> dims <- rep(list(quote(expr = )), nd - 1L)
>>> do.call(`[`, c(list(quote(x), quote(i)), dims, list(drop = FALSE)))
>>&...
2018 Jun 08
0
Subsetting the "ROW"s of an object
...for this. In S4Vectors, we
have extractROWS().
Michael
On Fri, Jun 8, 2018 at 8:45 AM, Hadley Wickham <h.wickham at gmail.com> wrote:
> Hi all,
>
> Is there a better to way to subset the ROWs (in the sense of NROW) of
> an vector, matrix, data frame or array than this?
>
> subset_ROW <- function(x, i) {
> nd <- length(dim(x))
> if (nd <= 1L) {
> x[i]
> } else {
> dims <- rep(list(quote(expr = )), nd - 1L)
> do.call(`[`, c(list(quote(x), quote(i)), dims, list(drop = FALSE)))
> }
> }
>
> subset_ROW(1:10, 4:6)
> #>...
2018 Jun 08
3
Subsetting the "ROW"s of an object
...recycled. (Maybe there is, or could be, ALTREP, support for
>> recycling)
>> Hadley
AFAICS, it is not an issue. Taking
arr <- array(rnorm(2^22),c(2^10,4,4,4))
as a test case
and using a function that will either use the literal code `x[i,,,,drop=FALSE]' or `eval(mc)':
subset_ROW4 <-
function(x, i, useLiteral=FALSE)
{
literal <- quote(x[i,,,,drop=FALSE])
mc <- quote(x[i])
nd <- max(1L, length(dim(x)))
mc[seq(4,length=nd-1L)] <- rep(TRUE, nd-1L)
mc[["drop"]] <- FALSE
if (useLiteral)
eval(literal)
else...
2018 Jun 08
3
Subsetting the "ROW"s of an object
...n 8, 2018, at 1:49 PM, Hadley Wickham <h.wickham at gmail.com> wrote:
>
> Hmmm, yes, there must be some special case in the C code to avoid
> recycling a length-1 logical vector:
Here is a version that (I think) handles Herve's issue of arrays having one or more 0 dimensions.
subset_ROW <-
function(x,i)
{
dims <- dim(x)
index_list <- which(dims[-1] != 0L) + 3
mc <- quote(x[i])
nd <- max(1L, length(dims))
mc[ index_list ] <- list(TRUE)
mc[[ nd + 3L ]] <- FALSE
names( mc )[ nd+3L ] <- "drop"
eval(mc)
}
Curiously...
2018 Jun 08
0
Subsetting the "ROW"s of an object
...kham <h.wickham at gmail.com> wrote:
>>
>> Hmmm, yes, there must be some special case in the C code to avoid
>> recycling a length-1 logical vector:
>
>
> Here is a version that (I think) handles Herve's issue of arrays having one or more 0 dimensions.
>
> subset_ROW <-
> function(x,i)
> {
> dims <- dim(x)
> index_list <- which(dims[-1] != 0L) + 3
> mc <- quote(x[i])
> nd <- max(1L, length(dims))
> mc[ index_list ] <- list(TRUE)
> mc[[ nd + 3L ]] <- FALSE
> names( mc )[ nd+3L ] <...
2018 Jun 08
4
Subsetting the "ROW"s of an object
...gt;>
>>
>> AFAICS, it is not an issue. Taking
>>
>> arr <- array(rnorm(2^22),c(2^10,4,4,4))
>>
>> as a test case
>>
>> and using a function that will either use the literal code `x[i,,,,drop=FALSE]' or `eval(mc)':
>>
>> subset_ROW4 <-
>> function(x, i, useLiteral=FALSE)
>> {
>> literal <- quote(x[i,,,,drop=FALSE])
>> mc <- quote(x[i])
>> nd <- max(1L, length(dim(x)))
>> mc[seq(4,length=nd-1L)] <- rep(TRUE, nd-1L)
>> mc[["drop"]] <- FALSE...