Re: [R] OK, next Q - a sort of factorial on a vector
Eric, On 2023-06-21 04:02, Eric Berger wrote: Hi Philip, In the decades since you learned R there have been some additions to the language. In particular, R now supports lambda functions. Applying this feature to Ivan's beautiful solution cuts down 7 characters (continuing his golfing analogy) unlist(lapply(seq_along(x), \(i) x[i] * x[-(1:i)])) Amazing! - it reminds me of the old days when there were competitions to write the smallest C programs . . Enjoy your return to R! Thanks! I think I was right to look at R first - it is exactly what I need I think - and the model I am thinking of shouldn't need any grunt that would require rewriting any functions in C or Rust or something . . P. On Tue, Jun 20, 2023 at 8:46 PM Philip Rhoades via R-help wrote: Ivan, On 2023-06-21 03:32, Ivan Krylov wrote: В Wed, 21 Jun 2023 03:13:52 +1000 Philip Rhoades via R-help пишет: This: !(1,2,3,4,5) would give this: (2,3,4,5, 6,8,10, 12,15, 20) Do you mean taking a product of every element of the vector with all following vector elements? A relatively straightforward way would be (given your vector stored in `x`): unlist(lapply(seq_along(x), function(i) x[i] * x[-(1:i)])) Perfect! (I'm sure it could be golfed further.) I will look at Sarah's suggestion too. and this: !(1,2,NA,4,5) would give this: (2,4,5, 8,10, 20) The previous solution seems to give your vector interspersed a bunch of NAs, so one way to continue would be to filter it using v[!is.na [1](v)]. Exactly! Thanks people - it would have taken forever to work that out myself (it has been decades since I looked at R). Phil. -- Philip Rhoades PO Box 896 Cowra NSW 2794 Australia E-mail: p...@pricom.com.au __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Links: -- [1] http://is.na -- Philip Rhoades PO Box 896 Cowra NSW 2794 Australia E-mail: p...@pricom.com.au __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] OK, next Q - a sort of factorial on a vector
Hi Philip, In the decades since you learned R there have been some additions to the language. In particular, R now supports lambda functions. Applying this feature to Ivan's beautiful solution cuts down 7 characters (continuing his golfing analogy) unlist(lapply(seq_along(x), \(i) x[i] * x[-(1:i)])) Enjoy your return to R! On Tue, Jun 20, 2023 at 8:46 PM Philip Rhoades via R-help < r-help@r-project.org> wrote: > Ivan, > > > On 2023-06-21 03:32, Ivan Krylov wrote: > > В Wed, 21 Jun 2023 03:13:52 +1000 > > Philip Rhoades via R-help пишет: > > > >> This: > >> > >>!(1,2,3,4,5) > >> > >> would give this: > >> > >>(2,3,4,5, 6,8,10, 12,15, 20) > > > > Do you mean taking a product of every element of the vector with all > > following vector elements? A relatively straightforward way would be > > (given your vector stored in `x`): > > > > unlist(lapply(seq_along(x), function(i) x[i] * x[-(1:i)])) > > > Perfect! > > > > (I'm sure it could be golfed further.) > > > I will look at Sarah's suggestion too. > > > >> and this: > >> > >>!(1,2,NA,4,5) > >> > >> would give this: > >> > >>(2,4,5, 8,10, 20) > > > > The previous solution seems to give your vector interspersed a bunch of > > NAs, so one way to continue would be to filter it using v[!is.na(v)]. > > > Exactly! > > Thanks people - it would have taken forever to work that out myself (it > has been decades since I looked at R). > > Phil. > -- > Philip Rhoades > > PO Box 896 > Cowra NSW 2794 > Australia > E-mail: p...@pricom.com.au > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] OK, next Q - a sort of factorial on a vector
Ivan, On 2023-06-21 03:32, Ivan Krylov wrote: В Wed, 21 Jun 2023 03:13:52 +1000 Philip Rhoades via R-help пишет: This: !(1,2,3,4,5) would give this: (2,3,4,5, 6,8,10, 12,15, 20) Do you mean taking a product of every element of the vector with all following vector elements? A relatively straightforward way would be (given your vector stored in `x`): unlist(lapply(seq_along(x), function(i) x[i] * x[-(1:i)])) Perfect! (I'm sure it could be golfed further.) I will look at Sarah's suggestion too. and this: !(1,2,NA,4,5) would give this: (2,4,5, 8,10, 20) The previous solution seems to give your vector interspersed a bunch of NAs, so one way to continue would be to filter it using v[!is.na(v)]. Exactly! Thanks people - it would have taken forever to work that out myself (it has been decades since I looked at R). Phil. -- Philip Rhoades PO Box 896 Cowra NSW 2794 Australia E-mail: p...@pricom.com.au __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] OK, next Q - a sort of factorial on a vector
vf <- function(x){ o <- outer(x, x) as.vector(na.omit(o[lower.tri(o)])) } vf(1:5) vf(c(1,2,NA,4,5)) Best, Uwe Ligges On 20.06.2023 19:13, Philip Rhoades via R-help wrote: People, What I mean is, is there an elegant way to do this: This: !(1,2,3,4,5) would give this: (2,3,4,5, 6,8,10, 12,15, 20) and this: !(1,2,NA,4,5) would give this: (2,4,5, 8,10, 20) ? Thanks! Phil. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] OK, next Q - a sort of factorial on a vector
Well, I think this is reasonable elegant, but ymmv. Turning it into a function and removing NA values is left for you. > x <- 1:5 > unlist(sapply(seq(1, length(x) - 1), function(i){x[i] * x[seq(i + 1, > length(x))]})) [1] 2 3 4 5 6 8 10 12 15 20 > > x <- c(1, 2, NA, 4, 5) > unlist(sapply(seq(1, length(x) - 1), function(i){x[i] * x[seq(i + 1, > length(x))]})) [1] 2 NA 4 5 NA 8 10 NA NA 20 Sarah On Tue, Jun 20, 2023 at 1:15 PM Philip Rhoades via R-help wrote: > > People, > > What I mean is, is there an elegant way to do this: > > This: > >!(1,2,3,4,5) > > would give this: > >(2,3,4,5, 6,8,10, 12,15, 20) > > and this: > >!(1,2,NA,4,5) > > would give this: > >(2,4,5, 8,10, 20) > > ? > > Thanks! > > Phil. > -- > Philip Rhoades > > PO Box 896 > Cowra NSW 2794 > Australia > E-mail: p...@pricom.com.au > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -- Sarah Goslee (she/her) http://www.numberwright.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] OK, next Q - a sort of factorial on a vector
В Wed, 21 Jun 2023 03:13:52 +1000 Philip Rhoades via R-help пишет: > This: > >!(1,2,3,4,5) > > would give this: > >(2,3,4,5, 6,8,10, 12,15, 20) Do you mean taking a product of every element of the vector with all following vector elements? A relatively straightforward way would be (given your vector stored in `x`): unlist(lapply(seq_along(x), function(i) x[i] * x[-(1:i)])) (I'm sure it could be golfed further.) > and this: > >!(1,2,NA,4,5) > > would give this: > >(2,4,5, 8,10, 20) The previous solution seems to give your vector interspersed a bunch of NAs, so one way to continue would be to filter it using v[!is.na(v)]. -- Best regards, Ivan __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] OK, next Q - a sort of factorial on a vector
People, What I mean is, is there an elegant way to do this: This: !(1,2,3,4,5) would give this: (2,3,4,5, 6,8,10, 12,15, 20) and this: !(1,2,NA,4,5) would give this: (2,4,5, 8,10, 20) ? Thanks! Phil. -- Philip Rhoades PO Box 896 Cowra NSW 2794 Australia E-mail: p...@pricom.com.au __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.