Skip to content
Prev 396579 / 398502 Next

Using the pipe, |>, syntax with "names<-"

Bert,

You need to consider LHS vs RHS functionality.

Before I start, I would have done your example setup lie this:

trio <- 1:3
z <- data.frame(a = trio, b = letters[trio])

Just kidding!

Syntactic sugar means you are calling this function:
function (x, value)  .Primitive("names<-")

Not particularly documented is a gimmick I just tried of supplying a second argument to the more routine function version:
one two
1   1   a
2   2   b
3   3   c

The above does not change z, but returns a new DF with new names.

In a pipeline, try this:

z <-
  z |>
  `names<-`( c("one","two"))
a b
1 1 a
2 2 b
3 3 c
+   z |>
+   `names<-`( c("one","two"))
one two
1   1   a
2   2   b
3   3   c





-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Bert Gunter
Sent: Saturday, July 20, 2024 4:47 PM
To: R-help <R-help at r-project.org>
Subject: Re: [R] Using the pipe, |>, syntax with "names<-"

With further fooling around, I realized that explicitly assigning my
last "solution" 'works'; i.e.

names(z)[2] <- "foo"

can be piped as:

 z <- z |>(\(x) "names<-"(x,value = "[<-"(names(x),2,'foo')))()
a foo
1 1   a
2 2   b
3 3   c

This is even awfuller than before. So my query still stands.

-- Bert
On Sat, Jul 20, 2024 at 1:14?PM Bert Gunter <bgunter.4567 at gmail.com> wrote:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.