Cómo capturar entero (0)?


Digamos que tenemos una declaración que produce integer(0), por ejemplo,

 a <- which(1:3 == 5)

¿Cuál es la forma más segura de atrapar esto?

Author: mbq, 2011-06-23

5 answers

Esa es la forma de R de imprimir un vector de longitud cero (un entero), por lo que podría probar que a es de longitud 0:

R> length(a)
[1] 0

Podría valer la pena repensar la estrategia que está utilizando para identificar qué elementos desea, pero sin más detalles específicos es difícil sugerir una estrategia alternativa.

 125
Author: Gavin Simpson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-23 08:30:45

Si es específicamente longitud cero enteros , entonces quieres algo como

is.integer0 <- function(x)
{
  is.integer(x) && length(x) == 0L
}

Compruébalo con:

is.integer0(integer(0)) #TRUE
is.integer0(0L)         #FALSE
is.integer0(numeric(0)) #FALSE

También puedes usar assertive para esto.

library(assertive)
x <- integer(0)
assert_is_integer(x)
assert_is_empty(x)
x <- 0L
assert_is_integer(x)
assert_is_empty(x)
## Error: is_empty : x has length 1, not 0.
x <- numeric(0)
assert_is_integer(x)
assert_is_empty(x)
## Error: is_integer : x is not of class 'integer'; it has class 'numeric'.
 14
Author: Richie Cotton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-07 13:47:16

Tal vez fuera de tema, pero R cuenta con dos funciones agradables, rápidas y conscientes de vacío para reducir los vectores lógicos {any y all:

if(any(x=='dolphin')) stop("Told you, no mammals!")
 12
Author: mbq,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-23 11:06:29
if ( length(a <- which(1:3 == 5) ) ) print(a)  else print("nothing returned for 'a'") 
#[1] "nothing returned for 'a'"

Pensándolo bien, creo que cualquiera es más hermoso que length(.):

 if ( any(a <- which(1:3 == 5) ) ) print(a)  else print("nothing returned for 'a'") 
 if ( any(a <- 1:3 == 5 ) ) print(a)  else print("nothing returned for 'a'") 
 7
Author: 42-,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-18 00:15:13

Inspirado por la respuesta de Andrie, puedes usar identical y evitar cualquier problema de atributo usando el hecho de que es el conjunto vacío de esa clase de objeto y combinarlo con un elemento de esa clase:

attr(a,"foo")<-"bar"

> identical(1L,c(a,1L))
[1] TRUE

O más generalmente:

is.empty <- function(x, mode=NULL){
    if (is.null(mode)) mode <- class(x)
    identical(vector(mode,1),c(x,vector(class(x),1)))
}

b <- numeric(0)

> is.empty(a)
[1] TRUE
> is.empty(a,"numeric")
[1] FALSE
> is.empty(b)
[1] TRUE
> is.empty(b,"integer")
[1] FALSE
 5
Author: James,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-23 11:17:15