# Weird behavior of is.numeric()

An interesting observation. Please note that I have absolutely no idea about why this happens. (at least not at the time of first writing this)

In our datalab, ingeniuously named “Datalab” (the one at KUB-North, because contrary to the labs at the libraries for social sciences, and for the humanities, we are not allowed to have a name), we were visited by at student.

She wanted to make a dose-response plot. Something about the concentration of something, giving some clotting of some blood. Or something…

Anyway, she wanted to do a 4-parameter logistic model. Thats nice, I had never heard about that before, but that is the adventure of running a datalab, and what makes it fun.

Of course there is a package for it, dr4pl. After an introduction to the wonderful world of dplyr, we set out to actually fit the model. This is a minimal working example of what happened:

``````library(tidyverse)
library(dr4pl)

data <- tibble(dose= 1:10, response = 2:11)

dr4pl(data, dose, response)
``````
```## Error in dr4pl.default(dose = dose, response = response, init.parm = init.parm, : Both doses and responses should be numeric.
```

WTF? “Both doses and responses should be numeric”?. But they are!

``````is.numeric(data\$dose)
``````
```##  TRUE
```
``````is.numeric(data\$response)
``````
```##  TRUE
```

The error is thrown by these lines in the source:

``````if(!is.numeric(dose)||!is.numeric(response)) {
stop("Both doses and responses should be numeric.")
}
``````

Lets try:

``````if(!is.numeric(data\$dose)||!is.numeric(data\$response)) {
stop("Both doses and responses should be numeric.")
} else {
print("Where did the problem go?")
}
``````
```##  "Where did the problem go?"
```

No idea. Did it disappear? No:

``````dr4pl(data, dose, response)
``````
```## Error in dr4pl.default(dose = dose, response = response, init.parm = init.parm, : Both doses and responses should be numeric.
```

Looking at the data might give us an idea of the source:

``````str(data)
``````
```## Classes 'tbl_df', 'tbl' and 'data.frame':    10 obs. of  2 variables:
##  \$ dose    : int  1 2 3 4 5 6 7 8 9 10
##  \$ response: int  2 3 4 5 6 7 8 9 10 11
```

Both dose and response are integers. Might that be the problem?

``````data <- tibble(dose= (1:10)*1.1, response = (2:11)*1.1)
dr4pl(data, dose, response)
``````
```## Error in dr4pl.default(dose = dose, response = response, init.parm = init.parm, : Both doses and responses should be numeric.
```

Nope. Both dose and response are now definitely numeric:

``````str(data)
``````
```## Classes 'tbl_df', 'tbl' and 'data.frame':    10 obs. of  2 variables:
##  \$ dose    : num  1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 11
##  \$ response: num  2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 11 12.1
```

But the problem persists.

Might this be the reason?

``````head(data,2)
``````
```## # A tibble: 2 x 2
##    dose response
##   <dbl>    <dbl>
## 1   1.1      2.2
## 2   2.2      3.3
```

It is a tibble. And the variables are reported to be doubles.

But according to the documentation:

“numeric is identical to double (and real)”

That should not be a problem then.

However. In desperation, I tried this:

``````c <- data %>%
as.data.frame() %>%
dr4pl(dose, response)
``````

And it works!

Why? Absolutely no idea!

Or do i?

The problem is that subsetting a tibble returns a new tibble. Well, subsetting a dataframe returns a dataframe as well?

It does. Unless you subset out a single variable or observation.

In a dataframe, df\$var returns a vektor, containing the values of var in df.

If, however, df is a tibble, df\$var will return a tibble, with just one variable.