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
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
2017 Jul 12
0
Extracting sentences with combinations of target words/terms from cancer patient text medical records
Hi Paul, Sounds like you have your answer, but for fun I thought I'd try solving your problem using only a regular expression query and base R. I believe this works: > txt <- "Patient had stage IV breast cancer. Nothing matches this sentence. Metastatic and breast match this sentence. French bike champion takes stage IV victory in Tour de France." > pattern <-
2017 Jul 12
2
Extracting sentences with combinations of target words/terms from cancer patient text medical records
Hi Bert, Thanks for your reply. It appears that I didn't replace the variable name "sampletxt" with the argument "x" in my function. I've corrected that and now my code seems to be working fine. Paul ________________________________ From: Bert Gunter <bgunter.4567 at gmail.com> Cc: R-help <r-help at r-project.org> Sent: Tuesday, July 11, 2017 2:00 PM
2011 Mar 29
1
new syntax: bash-like pipe operator
Dear R Community, One thing that always bugged me about R is the abundance of multi-level nested statements, like: cumsum(ifelse(c(1,diff(v)),1,0)) because: a) you have to read them inside out as opposed to left-to-right b) in the console you always have to go back and type the parenthesis if you want to nest your statement I kind of like the UNIX pipe operator as a very good abstraction of a