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
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,
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
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
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
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
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
##
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.
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
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.
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)
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
À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
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
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
15 matches
Mail list logo