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

2024-07-21 Thread avi.e.gross
As an intellectual exercise it can be reasonable to discuss these ways to use a pipe even in places where it may not have been seen as something anyone would even try to use it. In actual code, it is often better to not make overly cute constructions that others (or yourself a month later) will

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-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 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 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
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 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] Extract

2024-07-21 Thread Val
Thank you Bert! However, the last line of the script. dat |> names() |> _[4:8] <- paste0("s", 1:5) is giving me an error as shown below Error: pipe placeholder can only be used as a named argument Thank you! On Sat, Jul 20, 2024 at 7:41 PM Bert Gunter wrote: > > Val: > I wanted to add here a

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] Extract

2024-07-21 Thread Gabor Grothendieck
We can use read.table for a base R solution string <- read.table(text = dat$string, fill = TRUE, header = FALSE, na.strings = "") names(string) <- paste0("S", seq_along(string)) cbind(dat[-3], string) On Fri, Jul 19, 2024 at 12:52 PM Val wrote: > > Hi All, > > I want to extract new variables fro

Re: [R] Extract

2024-07-21 Thread Bert Gunter
I get no error. Please show the entirety of the code you used that produced the error. Also, are you using a current R version? I am, and if you are not, there might have been changes from your version to mine that caused the error. However, as you were already given satisfactory solutions before,

Re: [R] Extract

2024-07-21 Thread Bert Gunter
Nice! -- Let read.table do the work of handling the NA's. However, even simpler is to use the 'colnames' argument of read.table() for the column names no? string <- read.table(text = dat$string, fill = TRUE, header = FALSE, na.strings = "", col.names = paste0("s", 1:5)) dat <- cbind(da

Re: [R] Extract

2024-07-21 Thread Gabor Grothendieck
Fixing col.names=paste0("S", 1:5) assumes that there will be 5 columns and we may not want to do that. If there are only 3 fields in string, at the most, we may wish to generate only 3 columns. On Sun, Jul 21, 2024 at 2:20 PM Bert Gunter wrote: > > Nice! -- Let read.table do the work of handling

Re: [R] Extract

2024-07-21 Thread Bert Gunter
As always, good point. Here's a piped version of your code for those who are pipe afficianados. As I'm not very skilled with pipes, it might certainly be improved. dat <- dat$string |> read.table( text = _, fill = TRUE, header = FALSE, na.strings = "") |> (\(x)'names<-'(x,p