How to improve a for loop with selection

Good afternoon guys,
I'm trying to improve the "for" loop I've just written.
Checking online seems I have to use "sapply" instead of the "for" loop, but i don't know how to write it.

How can I improve this loop? considering length(A) is more than 800.000 elements

for(i in seq_along(a)) {
  ten_ <- AD$rain[i:(i+1)]
  tene <- sum(ten_)
  tenemean <- tene / 2
  ten[i] <- tenemean
  ten[is.na(ten)]<-0
}

thanks a lot guys, i'm still learning
:blush:


AD <- list()
AD$rain <-1:10
a <- 1:9
ten <- rep(NA,9)

for(i in seq_along(a)) {
  tene <- sum(AD$rain[i:(i+1)])
  tenemean <- tene / 2
  ten[i] <- tenemean
}
ten[is.na(ten)]<-0

###############

ten2 <- purrr::map_dbl(a,
                  ~{
                    sum(AD$rain[.:(.+1)]) / 2
                  })
ten2[is.na(ten2)]<-0
1 Like

Hi AndrePaind,

I was kind of expecting this question. I wasn't sure if you were using the for loop because you were trying to specificaly learn them. Anyway, people often disparag for loops because R is designed around the idea of 'vectorization'. This is what allows x <- 1:5; x + 1 to work s it does without using an explicit loop.

sapply and friends (or their tidyverse map equivalents) are often the best alternative to loops. In this case I would suggest you look at mapply or map2 / map2_***.

The you can avoid the whole AD[i:(i+1)] thing entirely.
Eg: mapply(function(a, b) seq(a, b), a = 1:5, b = 6:10)

Sorry about not coming forward with this sooner, I wasn't holding out on you, I just thought you were on a journey... :slight_smile:

1 Like

Thank You for the answer @nirgrahamuk but the code you suggest me, gives me a vector (ten2) full of zero.

Do you know where is the issue?

Thank You again

What should I change in what I posted to create the zero's as you do ?
FAQ: How to do a minimal reproducible example ( reprex ) for beginners

1 Like

:rofl: :rofl: that's funny.. yep i'm on my journey to learn R (I'm coming from Matlab so i've already a bit of knowledge of programming but I miss the "grammar")

@jmcvw thanks for your answers, it's good to see unconditional help here around.
It's easier if you can talk directly with someone instead of looking million of pages on internet.

ok so let's work with just a vector AD, which I'm gonna represent it as a list of random numbers (it's not a list of rand numbers in reality but that's ok)
If i'm running this, for example, the result is the same for every element and it should not.

a <- integer(800000)
AD <- runif (800000)
ten2 <- purrr::map_dbl(a,
                  ~{
                    sum(AD[.:(.+1)]) / 2
                  })
ten2[is.na(ten2)]<-0

whats the purpose of a, it seems to have zero information content, it just coincidentally happens to be as long as the AD object ?
you should probably omit it, the role it served in my first example was the indexes into AD to manipulate.

set.seed(42)
AD <- runif (800000)
ten2 <- purrr::map_dbl(seq_along(AD),
                       ~{
                         sum(AD[.:(.+1)]) / 2
                       })
ten2[is.na(ten2)]<-0

#an alternative using slider to slide a 2-length window over AD and apply a function to it
library(slider)
ten3 <- slide_dbl(AD,
                  .after = 1
                  ,.f = mean)

note that in the slider example ten3 differs from ten2 based on the differing treatment of the final value,
which in the first way, is NA mapped to 0 but in the second way, is the mean of the single number in the last position of AD

1 Like

amazing, thanks a lot @nirgrahamuk

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.