From henrik@p@rn @ending from ntnu@no  Thu Nov 29 09:20:12 2018
From: henrik@p@rn @ending from ntnu@no (=?utf-8?B?SGVucmlrIFDDpHJu?=)
Date: Thu, 29 Nov 2018 08:20:12 +0000
Subject: [Rd] named arguments discouraged in `[.data.frame` and
 `[<-.data.frame`
In-Reply-To: <CAF8bMcZhZJNbY7MYA=e9AQVCkR9v516b1z6-o9bgKOhq_Z9S1w@mail.gmail.com>
References: <fa0ff7bd9b544890be2ba300d5045473@it-ex11.win.ntnu.no>
 <CAF8bMcZhZJNbY7MYA=e9AQVCkR9v516b1z6-o9bgKOhq_Z9S1w@mail.gmail.com>
Message-ID: <ed14424f3ce0484cbb2062c8491cbcdb@it-ex11.win.ntnu.no>

Thanks Bill and Michael for taking the time to share your knowledge! 

As a further background to my question, here are two examples that I forgot to include in my original post (reminded by Michael's answer). I swapped the i and j arguments in `[.data.frame` and `[<-.data.frame`. With warnings, but else without (?) problem. Using Bill's data:

`[.data.frame`(x = d, i = 1, j = 2)
# [1] 12

`[.data.frame`(x = d, j = 2, i = 1)
# [1] 12

And similar for `[<-.data.frame` :
`[<-.data.frame`(x = d, i = 1, j = 2, value = 1122)
`[<-.data.frame`(x = d, j = 2, i = 1, value = 12)

Because this seemed to work, I made the hasty conclusion that argument switching _wasn't_ a problem for `[.data frame`, and that we could rely on exact matching on tags. But apparently not: despite that `[.data.frame` and `[<-.data.frame` are _not_ primitive functions, positional matching is done there as well. Sometimes. At least when 'x' argument is not first, as shown in Bill's examples. Obviously my "test" was insufficient...

Cheers,

Henrik



From: William Dunlap <wdunlap at tibco.com> 
Sent: Wednesday, November 28, 2018 9:10 PM
To: Henrik P?rn <henrik.parn at ntnu.no>
Cc: r-devel at r-project.org
Subject: Re: [Rd] named arguments discouraged in `[.data.frame` and `[<-.data.frame`

They can get bitten in the last two lines of this example, where the 'x' argument is not first:
> d <- data.frame(C1=c(r1=11,r2=21,r3=31), C2=c(12,22,32))
> d[1,1:2]
? ?C1 C2
r1 11 12
> `[`(d,j=1:2,i=1)
? ?C1 C2
r1 11 12
Warning message:
In `[.data.frame`(d, j = 1:2, i = 1) :
? named arguments other than 'drop' are discouraged
> `[`(j=1:2,d,i=1)
Error in (1:2)[d, i = 1] : incorrect number of dimensions
> do.call("[", list(j=1:2, i=1, x=d))
Error in 1:2[i = 1, x = list(C1 = c(11, 21, 31), C2 = c(12, 22, 32))] :
? incorrect number of dimensions

Bill Dunlap
TIBCO Software
wdunlap http://tibco.com


On Wed, Nov 28, 2018 at 11:30 AM Henrik P?rn <mailto:henrik.parn at ntnu.no> wrote:
tl;dr:

Why are named arguments discouraged in `[.data.frame`, `[<-.data.frame` and `[[.data.frame`?

(because this question is of the kind 'why is R designed like this?', I though R-devel would be more appropriate than R-help)

#############################

Background:

Now and then students presents there fancy functions like this: 

myfancyfun(d,12,0.3,0.2,500,1000,FALSE,TRUE,FALSE,TRUE,FALSE)

Incomprehensible. Thus, I encourage them to use spaces and name arguments, _at least_ when trying to communicate their code with others. Something like:

myfancyfun(data = d, n = 12, gamma = 0.3, prob = 0.2,
? ? ? ? ? ? ? ? ? ? ? size = 500, niter = 1000, model = FALSE,
? ? ? ? ? ? ? ? ? ? ?scale = TRUE, drop = FALSE, plot = TRUE, save = FALSE)


Then some overzealous students started to use named arguments everywhere. E-v-e-r-y-w-h-e-r-e. Even in the most basic situation when indexing vectors (as a subtle protest?), like:

vec <- 1:9

vec[i = 4]
`[`(x = vec, i = 4)

vec[[i = 4]]
`[[`(x = vec, i = 4)

vec[i = 4] <- 10
`[<-`(x = vec, i = 4, value = 10)

...or when indexing matrices:

m <- matrix(vec, ncol = 3)
m[i = 2, j = 2]
`[`(x = m, i = 2, j = 2)
# 5

m[i = 2, j = 2] <- 0
`[<-`(x = m, i = 2, j = 2, value = 0)

######

This practice indeed feels like overkill, but it didn't seem to hurt either. Until they used it on data frames. Then suddenly warnings appeared that named arguments are discouraged:

d <- data.frame(m)

d[[i = "X2"]]
# [1] 4 5 6
# Warning message:
# In `[[.data.frame`(d, i = "X2") :
#? named arguments other than 'exact' are discouraged

d[i = 2, j = 2]
# [1] 0
# Warning message:
# In `[.data.frame`(d, i = 2, j = 2) :
#? named arguments other than 'drop' are discouraged

d[i = 2, j = 2] <- 5
# Warning message:
# In `[<-.data.frame`(`*tmp*`, i = 2, j = 2, value = 5) :
#? named arguments are discouraged


##################################

Of course I could tell them "don't do it, it's overkill and not common practice" or "it's just a warning, don't worry". However, I assume the warnings are there for a good reason.

So how do I explain to the students that named arguments are actively discouraged in `[.data.frame` and `[<-.data.frame`, but not in `[` and `[<-`? When will they get bitten?

______________________________________________
mailto:R-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] named arguments discouraged in `[.data.frame` and `[<-.data.frame`
=?utf-8?B?SGVucmlrIFDDpHJu?=