Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-21 Thread Gabor Grothendieck
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 p

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-21 Thread CALUM POLWART
The tidy solution is rename literally: z |> rename(foo = 2) Or you could do it with other functions z |> select ( 1, foo = 2) Or z |> mutate( foo = 2 ) |> # untested (always worry that makes the whole column 2) select (-2) But that's akin to z$foo <- z[2] z[2] <- null On Sun, 21 Jul 2024,

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-21 Thread Bert Gunter
Thanks, Calum. That was exactly what Duncan Murdoch proposed earlier in this thread, except, of course, he had to explicitly write the function first. -- Bert On Sun, Jul 21, 2024 at 8:12 AM CALUM POLWART wrote: > > The tidy solution is rename > > literally: > > z |> rename(foo = 2) > > Or you

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-21 Thread Gabor Grothendieck
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 wrote: > > hmmm... > But note that you still used the nested assignment, names()[2] <- > "foo", to circumvent R's pipe limitations, w

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-21 Thread Bert Gunter
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 wrote: > > Wow! > Yes, this

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-21 Thread Bert Gunter
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

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-21 Thread Gabor Grothendieck
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 ##

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread Duncan Murdoch
On 2024-07-20 6:02 p.m., Iris Simmons wrote: z <- data.frame(a = 1:3, b = letters[1:3]) z |> names() |> _[2] <- "foo" z That's a great suggestion! Duncan Murdoch __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread Bert Gunter
Iris's reply is what I was looking for. Many thanks -- I can now sleep tonight! Both Rui's and Duncan's responses merely hid what I wanted to avoid. I hope that I did not occupy much of your times on my useless question and rather pathetic attempts at an answer. Cheers, Bert On Sat, Jul 20, 2

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread Iris Simmons
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 wrote: > > With further fooling around, I realized that explicitly assigning my > last "solution" 'works'; i.e.

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread avi.e.gross
e----- From: R-help On Behalf Of Bert Gunter Sent: Saturday, July 20, 2024 4:47 PM To: R-help 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)

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread Duncan Murdoch
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

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread Rui Barradas
À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

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread Bert Gunter
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 stand

Re: [R] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread Bert Gunter
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 wrote: > > This post is likely pretty useless; it is motivated by a recent post > from "Val" that was elegantly answered using Ti

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

2024-07-20 Thread Bert Gunter
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 int