Re: [R] slowness when I use a list comprehension

2024-06-16 Thread Laurent Rhelp
Thank you for this solution which is faster than the two for loops. gloop <- function(N1,N2,ratio_sampling,vec1,vec2){   ix <- seq_along(vec2)   S_diff2 <- sapply(seq_len(N1-(N2-1)*ratio_sampling), \(j)     sum((vec1[(ix-1)*ratio_sampling+j] - vec2[ix])**2))   return(S_diff2)

Re: [R] slowness when I use a list comprehension

2024-06-16 Thread Gabor Grothendieck
This can be vectorized. Try ix <- seq_along(vec2) S_diff2 <- sapply(seq_len(N1-(N2-1)*ratio_sampling), \(j) sum((vec1[(ix-1)*ratio_sampling+j] - vec2[ix])**2)) On Sun, Jun 16, 2024 at 11:27 AM Laurent Rhelp wrote: > > Dear RHelp-list, > > I try to use the package comprehenr to replace a

Re: [R] slowness when I use a list comprehension

2024-06-16 Thread Laurent Rhelp
Avi and Jeff, Thank you very much for your answers. I did not think I would get such an interessing answer when I asked my question. In fact, I discovered recently the list comprehension reading some python code and I was seduced but the compact notation so I decided to do an exercice on an

Re: [R] slowness when I use a list comprehension

2024-06-16 Thread avi.e.gross
I fully agree with Jeff that the best way to use ANY language is to evaluate the language in terms of not just the capabilities it offers but also the philosophy behind what it was created for and how people do things and just grok it and use it mostly in the way intended. I do that with all the

Re: [R] slowness when I use a list comprehension

2024-06-16 Thread Jeff Newmiller via R-help
I would be more strong on this advice: learn to think in R, rather than thinking in Python, when programming in R. R has atomic vectors... Python does not (until you import a package that implements them). I find that while it is possible to import R thinking into Python, Python programmers

Re: [R] slowness when I use a list comprehension

2024-06-16 Thread avi.e.gross
Laurent, Thank you for introducing me to a package I did not know existed as I use features like list comprehension in python all the time and could see using it in R now that I know it is available. As to why you see your example as slow, I see you used a fairly complex and nested expression

[R] slowness when I use a list comprehension

2024-06-16 Thread Laurent Rhelp
Dear RHelp-list,    I try to use the package comprehenr to replace a for loop by a list comprehension.  I wrote the code but I certainly miss something because it is very slower compared to the for loops. May you please explain to me why the list comprehension is slower in my case. Here

Re: [R] code for year month day hr format

2024-06-16 Thread Rolf Turner
On Sun, 16 Jun 2024 08:33:03 +0100 Rui Barradas wrote: > Hello, > > There is an error in your new code: > > > paste YEAR with DOY, not with HR. > > > As for the rest, is your real data like the one you posted before? > If it is then I don't see anything wrong with my (tested) solution.

Re: [R] code for year month day hr format

2024-06-16 Thread Rui Barradas
Às 21:42 de 15/06/2024, Jibrin Alhassan escreveu: Thank you Rui. I ran the following script df1 <- read.table("solar_hour", header = TRUE) df1$date <- as.Date(paste(df1$year, df1$hour), format = "%Y %j", origin = "2012-08-01-0") df2 <- df1[c("date", "IMF", "SWS", "SSN", "Dst", "f10")]