similar to: Using the pipe, |>, syntax with "names<-"

Displaying 20 results from an estimated 30000 matches similar to: "Using the pipe, |>, syntax with "names<-""

2024 Jul 21
3
Using the pipe, |>, syntax with "names<-"
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 solution just for completeness? Best, Bert On Sun, Jul 21, 2024 at 7:48?AM Gabor
2024 Jul 21
1
Using the pipe, |>, syntax with "names<-"
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 <bgunter.4567 at gmail.com> wrote: > > Wow! > Yes, this is very clever -- way too clever for
2024 Jul 21
1
Using the pipe, |>, syntax with "names<-"
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 ## a foo ## 1 1 a ## 2 2 b ## 3 3 c On Sat, Jul 20, 2024 at
2024 Jul 21
1
Using the pipe, |>, syntax with "names<-"
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 <bgunter.4567 at gmail.com> wrote: > > hmmm... > But note that you still used the nested assignment, names()[2] <- > "foo", to circumvent R's pipe limitations, which is exactly
2024 Jul 20
4
Using the pipe, |>, syntax with "names<-"
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 stands. -- Bert On Sat, Jul
2024 Jul 20
1
Using the pipe, |>, syntax with "names<-"
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 <bgunter.4567 at gmail.com> wrote: > > 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
2024 Jul 20
2
Using the pipe, |>, syntax with "names<-"
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 <bgunter.4567 at gmail.com> wrote: > > With further fooling around, I realized that explicitly assigning my > last "solution" 'works'; i.e. > >
2024 Jul 21
0
Using the pipe, |>, syntax with "names<-"
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 <polc1410 at gmail.com> wrote: > > The tidy solution is rename > > literally: > > z |> rename(foo = 2) > > Or you could do it with other functions > > z
2024 Jul 21
2
[External] Using the pipe, |>, syntax with "names<-"
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) <- "foo" > z a foo 1 1 a 2 2
2024 Jul 22
1
Extract
Thanks. I found this to be quite informative and a nice example of how useful R-Help can be as a resource for R users. Best, Bert On Mon, Jul 22, 2024 at 4:50?AM Gabor Grothendieck <ggrothendieck at gmail.com> wrote: > > Base R. Regarding code improvements: > > 1. Personally I find (\(...) ...)() notation hard to read (although by > placing (\(x), the body and )() on 3
2012 Mar 30
4
list assignment syntax?
Dear R wizards: is there a clean way to assign to elements in a list? what I would like to do, in pseudo R+perl notation is f <- function(a,b) list(a+b,a-b) (c,d) <- f(1,2) and have c be assigned 1+2 and d be assigned 1-2. right now, I use the clunky x <- f(1,2) c <- x[[1]] d <- x[[2]] rm(x) which seems awful. is there a nicer syntax? regards, /iaw ---- Ivo Welch
2024 Jul 22
3
Extract
Base R. Regarding code improvements: 1. Personally I find (\(...) ...)() notation hard to read (although by placing (\(x), the body and )() on 3 separate lines it can be improved somewhat). Instead let us use a named function. The name of the function can also serve to self document the code. 2. The use of dat both at the start of the pipeline and then again within a later step of the pipeline
2024 Jul 20
1
[External] Using the pipe, |>, syntax with "names<-"
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 *only with* Iris's solution. Hers should be shown whether or not
2025 Mar 30
1
Creating model formulas programmatically
Another solution. reformulate + substitute + as.formula: substitute(~ (.)^2, list(. = reformulate(somenames)[[2]])) |> as.formula() On Sat, Mar 29, 2025 at 5:31?PM Bert Gunter <bgunter.4567 at gmail.com> wrote: > > Note: I am almost certain that this has been asked and answered here > before, so my apologies for the redundant query. > > I also know that there are several
2025 Mar 29
4
Creating model formulas programmatically
Note: I am almost certain that this has been asked and answered here before, so my apologies for the redundant query. I also know that there are several packages that will do this, but I wish to do it using base R functions only (see below). The query: Suppose I have a character vector of names like this: somenames <- c("Heigh", "Ho", "Silver", "Away")
2025 Mar 30
1
Creating model formulas programmatically
Gabor, Duncan, et. al. 1. Thank you for your great comments and solutions. This is what I was hoping for! 2. Duncan: I completely agree with your criticisms. In fact, I realized the for() loop only needed the <- assignment, but your comment is important to note. However, I didn't like the for() loop either; I *much* preferred your Reduce() solution which is exactly the sort of elegant
2024 Jul 21
1
Extract
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,paste0("s", seq_along(x))))() |>
2024 Jul 21
1
Extract
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 <bgunter.4567 at gmail.com> wrote: > > Nice! -- Let read.table do the work of handling the NA's. > However, even simpler is to use
2013 May 13
2
reduce three columns to one with the colnames
Ein eingebundener Text mit undefiniertem Zeichensatz wurde abgetrennt. Name: nicht verf?gbar URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130513/fe848ce7/attachment.pl>
2024 Jul 21
1
Extract
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(dat, string) -- Bert On Sun, Jul 21, 2024 at 10:16?AM Gabor