From @nto|ne@|@br| @end|ng |rom gm@||@com  Fri Jun 21 15:08:13 2019
From: @nto|ne@|@br| @end|ng |rom gm@||@com (Ant F)
Date: Fri, 21 Jun 2019 15:08:13 +0200
Subject: [Rd] head with non integer n returns confusing output
Message-ID: <CAEKh8ujebf5oKxWbSHLgyU1cNtbh7=U0dp6k_nne=-gUenUdgQ@mail.gmail.com>

Dear all,

`head()` returns a problematic output when a character is fed to its `n`
parameter.

doubles and logicals are converted to integer as if `as.integer` was used,
which I think is intuitive enough :

```
head(1:10, 4.1)  # [1] 1 2 3 4
head(1:10, 4.9)  # [1] 1 2 3 4
head(1:10, TRUE)  # 1
head(1:10, FALSE) # integer(0)
```

But characters have a stranger behavior :

```
head(1:10, "0") # integer(0)
head(1:10, "0.1") # integer(0)
head(1:10, "1") # [1] 1
head(1:10, "1.2") # [1] 1
head(1:10, "2") # [1] [1]  1  2  3  4  5  6  7  8  9 10
head(1:10, "-1") # Error in length(x) + n : non-numeric argument to binary
operator
head(1:10, "-0") # Error in length(x) + n : non-numeric argument to binary
operator
head(1:10, "foo") # [1]  1  2  3  4  5  6  7  8  9 10
```

When forgetting to convert user input to numeric this can lead to an
unexpected and inconsistent result.

I would suggest either using `as.integer` consistently on the input, or
having a consistent error for all character input.

`n = NA` also a returns a somewhat misleading error as it demands a logical
input while the top level function requires an integer input.

```
head(1:10, NA)  # same output for head(1:10, NA_integer_)
# Error in if (n < 0L) max(length(x) + n, 0L) else min(n, length(x)) :
#   missing value where TRUE/FALSE needed
```

Kind regards,

Antoine

	[[alternative HTML version deleted]]



[Rd] head with non integer n returns confusing output
Ant F