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')))()> za 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:> > Nope, I still got it wrong: None of my approaches work. :( > > So my query remains: how to do it via piping with |> ? > > Bert > > > On Sat, Jul 20, 2024 at 1:06?PM Bert Gunter <bgunter.4567 at 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
?s 21:46 de 20/07/2024, Bert Gunter escreveu:> 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')))() >> z > 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: >> >> Nope, I still got it wrong: None of my approaches work. :( >> >> So my query remains: how to do it via piping with |> ? >> >> Bert >> >> >> On Sat, Jul 20, 2024 at 1:06?PM Bert Gunter <bgunter.4567 at 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 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.Hello, This is not exactly the same but in one of your attempts all you have to do is to return x. The following works and does something. z |> (\(x){names(x)[2] <- "foo";x})() # a foo # 1 1 a # 2 2 b # 3 3 c Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
I suspect that you would want to define a function which was aware of the limitations of piping to handle this. For example: rename <- function(x, col, newname) { names(x)[col] <- newname x } Then z |> rename(2, "foo") would be fine. Duncan Murdoch On 2024-07-20 4:46 p.m., Bert Gunter wrote:> 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')))() >> z > 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: >> >> Nope, I still got it wrong: None of my approaches work. :( >> >> So my query remains: how to do it via piping with |> ? >> >> Bert >> >> >> On Sat, Jul 20, 2024 at 1:06?PM Bert Gunter <bgunter.4567 at 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 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.
It should be written more like this: ```R z <- data.frame(a = 1:3, b = letters[1:3]) z |> names() |> _[2] <- "foo" z ``` Regards, Iris On Sat, Jul 20, 2024 at 4:47?PM Bert Gunter <bgunter.4567 at gmail.com> wrote:> > 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')))() > > z > 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: > > > > Nope, I still got it wrong: None of my approaches work. :( > > > > So my query remains: how to do it via piping with |> ? > > > > Bert > > > > > > On Sat, Jul 20, 2024 at 1:06?PM Bert Gunter <bgunter.4567 at 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 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.
@vi@e@gross m@iii@g oii gm@ii@com
2024-Jul-20 22:07 UTC
[R] 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:> `names<-`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:> `names<-`(z, c("one","two"))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"))> za b 1 1 a 2 2 b 3 3 c> z <-+ z |> + `names<-`( c("one","two"))> zone 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')))()> za 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:> > Nope, I still got it wrong: None of my approaches work. :( > > So my query remains: how to do it via piping with |> ? > > Bert > > > On Sat, Jul 20, 2024 at 1:06?PM Bert Gunter <bgunter.4567 at 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 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.