I think that the simplicity of setNames is hard to beat:
z |> setNames( c( "a", "foo" ) )
and if you are determined not to load dplyr then
column_rename <- function( DF, map ) {
on <- names( DF )
on[ match( map, on ) ] <- names( map )
names( DF ) <- on
DF
}
is more robust to column reor
The main challenge in Bert's original problem is that `[` and `[<-` cannot
be called in a pipeline. The obvious solution is to define named versions,
e.g.:
elt <- `[`
`elt<-` <- `[<-`
Then,
> z <- data.frame(a = 1:3, b = letters[1:3])
> z |> names() |> elt(2)
[1] "b"
> z |> names() |> elt(2) <-
Val:
I wanted to add here a base R solution to your problem that I realize
you can happily ignore. However, in the course of puzzling over how to
do it using the R native pipe syntax ("|>") , I learned some new stuff
that I thought others might find useful, and it seemed sensible to
keep the code w
I second Rich's excellent suggestion.
As with all elegant solutions, Iris's clicked on the wee light bulb in
my brain, and I realized that a slightly more verbose, but perhaps
more enlightening, alternative may be:
z |> attr("names") |> _[2] <- "foo"
However, I would add this as an example *onl
I think Iris's solution should be added to the help file: ?|>
there are no examples there now that show assignment or replacement using the
"_"
> On Jul 20, 2024, at 18:21, Duncan Murdoch wrote:
>
> On 2024-07-20 6:02 p.m., Iris Simmons wrote:
>> z <- data.frame(a = 1:3, b = letters[1:3])
>> z |
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.
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<-"
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
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
14 matches
Mail list logo