Proposition to stats::setNames() (include formula)

Hey. I understand that it is better to use setNames in a pipeline of loading data (Like in Tidymodel's getting started)

But currently the function is pretty limited, only accepting character vectors. But it would make a lot of sense to allow formula notation in my opinion.

named_vec <- c(one = 25, two = 35, three = 45)

# Current:
setNames(named_vec, c("ONE", "TWO", "THREE"))
# Desired:
# Based on this logic:

would love to hear community's thought and R team's people!

{stats} is generally above the pay grade of the regular participants here, I don't recall ever seeing anyone from the R Core Team here. However, for the core functionality like {stats} there is a Slack where this might get some potential. Although it does seem trivial to roll it ones self.

named_vec <- c(one = 25, two = 35, three = 45)

set_names <- function(x) setNames(x, toupper(names(x)))

#>    25    35    45

Created on 2023-06-13 with reprex v2.0.2

Or a y parameter could replace toupper

1 Like

In base R this can also be achieved by another form of composition

(named_vec <- c(one = 25, 
                two = 35, 
                three = 45))

setNames(named_vec, sapply(
1 Like

Thanks @technocrat
I know there are ways around (like the map case), it just feels so trivial to incorporate this in the natural behaviour of setNames, wouldn't you agree? 'to upper' is only one example, but there are many use cases for formula, and it exists in so many other R functions..

Didn't know about the Slack channel though. I'll join to see what's happening there and if this suggestion can be meaningful there.

nirgrahamuk Thanks as well. I guess I am not asking for a solution (I provided a working example after all), but it was merely a proposition to R team after I encountered this function in a new article of the Tidymodels, which is a new framework I guess a lot of smart people are working on. Always nice to see other examples anyway.

regards X2!

1 Like

R's formula syntax typically occurs in context where some number of terms from within set of data are picked out, and relations identified, typically this is model fitting, or constructing graphics; I think formula syntax is overkill for standard data manipulation contexts; in fact tidyverse is moving away, the best practice in purrr::map used to be formula; but since R introduce anonymous function syntax i.e. \(x), the best practice is now not to use ~ but \(x) or equivalent

Be interesting to see the reception there. Just saying because it probably goes back 25 years and the authors are heavy hitters.

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.