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

2024-07-21 Thread avi.e.gross
gine an implementation where you constantly put in ".placeHolder." as clearer. -Original Message- From: R-help On Behalf Of Deepayan Sarkar Sent: Sunday, July 21, 2024 1:08 AM To: Bert Gunter Cc: R-help Subject: Re: [R] [External] Using the pipe, |>, syntax with "names&l

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

2024-07-20 Thread Jeff Newmiller via R-help
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

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

2024-07-20 Thread Deepayan Sarkar
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) <-

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

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

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

2024-07-20 Thread Richard M. Heiberger
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 |