That was supposed to be z |> list(x = _) |> within(names(x) <- replace(names(x), 2, "foo")) |> _$x
but I really see no advantage over z |> list(x = _) |> within(names(x)[2] <- "foo") |> _$x Regarding the z |> names() |> _[2] <- "foo" idiom, while it is clever, and well illustrates what is possible with base R pipes that might not be initially expected, I think it should be discouraged as not in the spirit of pipes which is a more functional approach to programming. Such an approach ought to be non-destructive and should pass on the result to the next step in the pipeline. These criteria are not satisfied by it. On Sun, Jul 21, 2024 at 11:17 AM Gabor Grothendieck <ggrothendi...@gmail.com> wrote: > > If you object to names(x)[2]<- ... then use replace: > > z |> list(x = _) |> within(replace(names(x), 2, "foo")) |> _$x > > On Sun, Jul 21, 2024 at 11:10 AM Bert Gunter <bgunter.4...@gmail.com> wrote: > > > > hmmm... > > But note that you still used the nested assignment, names()[2] <- > > "foo", to circumvent R's pipe limitations, which is exactly what > > Iris's solution avoids. So I think I was overawed by your cleverness > > ;-) > > > > Best, > > Bert > > > > > > On Sun, Jul 21, 2024 at 8:01 AM Bert Gunter <bgunter.4...@gmail.com> wrote: > > > > > > Wow! > > > Yes, this is very clever -- way too clever for me -- and meets my > > > criteria for a solution. > > > > > > I think it's also another piece of evidence of why piping in base R is > > > not suited for complex/nested assignments, as discussed in Deepayan's > > > response. > > > > > > Maybe someone could offer a better Tidydata piping solution just for > > > completeness? > > > > > > Best, > > > Bert > > > > > > On Sun, Jul 21, 2024 at 7:48 AM Gabor Grothendieck > > > <ggrothendi...@gmail.com> wrote: > > > > > > > > This > > > > - is non-destructive (does not change z) > > > > - passes the renamed z onto further pipe legs > > > > - does not use \(x)... > > > > > > > > It works by boxing z, operating on the boxed version and then unboxing > > > > it. > > > > > > > > z <- data.frame(a = 1:3, b = letters[1:3]) > > > > z |> list(x = _) |> within(names(x)[2] <- "foo") |> _$x > > > > ## a foo > > > > ## 1 1 a > > > > ## 2 2 b > > > > ## 3 3 c > > > > > > > > On Sat, Jul 20, 2024 at 4:07 PM Bert Gunter <bgunter.4...@gmail.com> > > > > wrote: > > > > > > > > > > This post is likely pretty useless; it is motivated by a recent post > > > > > from "Val" that was elegantly answered using Tidyverse constructs, but > > > > > I wondered how to do it using base R only. Along the way, I ran into > > > > > the following question to which I think my answer (below) is pretty > > > > > awful. I would be interested in more elegant base R approaches. So... > > > > > > > > > > z <- data.frame(a = 1:3, b = letters[1:3]) > > > > > > z > > > > > a h > > > > > 1 1 a > > > > > 2 2 b > > > > > 3 3 c > > > > > > > > > > Suppose I want to change the name of the second column of z from 'b' > > > > > to 'foo' . This is very easy using nested function syntax by: > > > > > > > > > > names(z)[2] <- "foo" > > > > > > z > > > > > a foo > > > > > 1 1 a > > > > > 2 2 b > > > > > 3 3 c > > > > > > > > > > Now suppose I wanted to do this using |> syntax, along the lines of: > > > > > > > > > > z |> names()[2] <- "foo" ## throws an error > > > > > > > > > > Slightly fancier is: > > > > > > > > > > z |> (\(x)names(x)[2] <- "b")() > > > > > ## does nothing, but does not throw an error. > > > > > > > > > > However, the following, which resulted from a more careful read of > > > > > ?names works (after changing the name of the second column back to "b" > > > > > of course): > > > > > > > > > > z |>(\(x) "names<-"(x,value = "[<-"(names(x),2,'foo')))() > > > > > >z > > > > > a foo > > > > > 1 1 a > > > > > 2 2 b > > > > > 3 3 c > > > > > > > > > > This qualifies to me as "pretty awful." I'm sure there are better ways > > > > > to do this using pipe syntax, so I would appreciate any better > > > > > approaches. > > > > > > > > > > Best, > > > > > Bert > > > > > > > > > > ______________________________________________ > > > > > R-help@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. > > > > > > > > > > > > > > > > -- > > > > Statistics & Software Consulting > > > > GKX Group, GKX Associates Inc. > > > > tel: 1-877-GKX-GROUP > > > > email: ggrothendieck at gmail.com > > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ R-help@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.