[Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hadley Wickham
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(

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Iñaki Úcar
El vie., 8 jun. 2018 a las 17:46, Hadley Wickham () escribió: > > 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] > } el

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Iñaki Úcar
Sorry, without remnants from other attempts: subset_ROW <- function(x, i) { nd <- length(dim(x)) if (nd <= 1L) return(x[i]) apply(x, 2:nd, `[`, i, drop=FALSE) } El vie., 8 jun. 2018 a las 19:07, Iñaki Úcar () escribió: > > El vie., 8 jun. 2018 a las 17:46, Hadley Wickham > () escribió: >

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Michael Lawrence
There probably should be an abstraction for this. In S4Vectors, we have extractROWS(). Michael On Fri, Jun 8, 2018 at 8:45 AM, Hadley Wickham 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_

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Berry, Charles
> On Jun 8, 2018, at 8:45 AM, Hadley Wickham 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) { > n

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hadley Wickham
I suspect this will have suboptimal performance since the TRUEs will get recycled. (Maybe there is, or could be, ALTREP, support for recycling) Hadley On Fri, Jun 8, 2018 at 10:16 AM, Berry, Charles wrote: > > >> On Jun 8, 2018, at 8:45 AM, Hadley Wickham wrote: >> >> Hi all, >> >> Is there a be

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hervé Pagès
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

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hervé Pagès
On 06/08/2018 10:32 AM, Hervé Pagès wrote: 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://urldefense.proofpoint.com/v2/url?u=ht

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hervé Pagès
Also the TRUEs cause problems if some dimensions are 0: > matrix(raw(0), nrow=5, ncol=0)[1:3 , TRUE] Error in matrix(raw(0), nrow = 5, ncol = 0)[1:3, TRUE] : (subscript) logical subscript too long H. On 06/08/2018 10:29 AM, Hadley Wickham wrote: I suspect this will have suboptimal perf

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Berry, Charles
> On Jun 8, 2018, at 10:37 AM, Hervé Pagès wrote: > > Also the TRUEs cause problems if some dimensions are 0: > > > matrix(raw(0), nrow=5, ncol=0)[1:3 , TRUE] > Error in matrix(raw(0), nrow = 5, ncol = 0)[1:3, TRUE] : >(subscript) logical subscript too long OK. But this is easy enough t

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hadley Wickham
On Fri, Jun 8, 2018 at 11:38 AM, Berry, Charles wrote: > > >> On Jun 8, 2018, at 10:37 AM, Hervé Pagès wrote: >> >> Also the TRUEs cause problems if some dimensions are 0: >> >> > matrix(raw(0), nrow=5, ncol=0)[1:3 , TRUE] >> Error in matrix(raw(0), nrow = 5, ncol = 0)[1:3, TRUE] : >>(subsc

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hervé Pagès
A missing subscript is still preferable to a TRUE though because it carries the meaning "take it all". A TRUE also achieves this but via implicit recycling. For example x[ , , ] and x[TRUE, TRUE, TRUE] achieve the same thing (if length(x) != 0) and are both no-ops but the subsetting code gets a ch

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Berry, Charles
> On Jun 8, 2018, at 11:52 AM, Hadley Wickham wrote: > > On Fri, Jun 8, 2018 at 11:38 AM, Berry, Charles wrote: >> >> >>> On Jun 8, 2018, at 10:37 AM, Hervé Pagès wrote: >>> >>> Also the TRUEs cause problems if some dimensions are 0: >>> matrix(raw(0), nrow=5, ncol=0)[1:3 , TRUE] >>>

[Rd] Date class shows Inf as NA; this confuses the use of is.na()

2018-06-08 Thread Werner Grundlingh
In the following example, the date class shows Inf as NA > as_date(Inf, origin = '1970-01-01') [1] NA This is misleading as is.na() reports incorrectly > is.na(as_date(Inf, origin = '1970-01-01')) [1] FALSE The correct approach here would probably to have an Inf (and -Inf) *displayed* rather th

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hadley Wickham
Hmmm, yes, there must be some special case in the C code to avoid recycling a length-1 logical vector: dims <- c(4, 4, 4, 1e5) arr <- array(rnorm(prod(dims)), dims) dim(arr) #> [1] 4 4 4 10 i <- c(1, 3) bench::mark( arr[i, TRUE, TRUE, TRUE], arr[i, , , ] )[c("expression",

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Michael Lawrence
Actually, it's sort of the opposite. Everything becomes a sequence of integers internally, even when the argument is missing. So the same amount of work is done, basically. ALTREP will let us improve this sort of thing. Michael On Fri, Jun 8, 2018 at 1:49 PM, Hadley Wickham wrote: > Hmmm, yes, t

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hervé Pagès
The C code for subsetting doesn't need to recycle a logical subscript. It only needs to walk on it and start again at the beginning of the vector when it reaches the end. Not exactly the same as detecting the "take everything along that dimension" situation though. x[TRUE, TRUE, TRUE] triggers the

Re: [Rd] Date class shows Inf as NA; this confuses the use of is.na()

2018-06-08 Thread MacQueen, Don via R-devel
> as_date Error: object 'as_date' not found Must be from some not-named package... But don't confuse the format of an object when printed with its underlying value: > as.Date(Inf,origin = '1970-01-01') [1] NA > str(as.Date(Inf,origin = '1970-01-01')) Date[1:1], format: NA > as.numeric(as.Dat

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Berry, Charles
> On Jun 8, 2018, at 1:49 PM, Hadley Wickham 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)

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hadley Wickham
On Fri, Jun 8, 2018 at 2:09 PM, Berry, Charles wrote: > > >> On Jun 8, 2018, at 1:49 PM, Hadley Wickham 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 hav

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Hervé Pagès
On 06/08/2018 02:15 PM, Hadley Wickham wrote: On Fri, Jun 8, 2018 at 2:09 PM, Berry, Charles wrote: On Jun 8, 2018, at 1:49 PM, Hadley Wickham 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

Re: [Rd] Subsetting the "ROW"s of an object

2018-06-08 Thread Berry, Charles
> On Jun 8, 2018, at 2:15 PM, Hadley Wickham wrote: > > On Fri, Jun 8, 2018 at 2:09 PM, Berry, Charles wrote: >> >> >>> On Jun 8, 2018, at 1:49 PM, Hadley Wickham wrote: >>> >>> Hmmm, yes, there must be some special case in the C code to avoid >>> recycling a length-1 logical vector: >> >

Re: [Rd] Date class shows Inf as NA; this confuses the use of is.na()

2018-06-08 Thread Werner Grundlingh
Indeed. as_date is from lubridate, but the same holds for as.Date. The output and it's interpretation should be consistent, otherwise it leads to confusion when programming. I understand that the difference exists after asking a question on Stack Overflow: https://stackoverflow.com/q/50766089/91