# (Beginner) How to average the data every n rows with the specific conditions

Hello R community, this is my first time to post on this board, please help or recommend me some.

My question is "how to average the data every 6 rows with the specific conditions"

In my case, I have to average the data every 6 rows but in each 6 rows if there are no data over 3 rows in 1 set (1 set = 6 rows), we would not count or average that set (show blank results)!!

Moreover, when we average we will divided by itself, if in one set showed 5 value, we divided by 5.

I will really appreciate it
As I am beginner and I just start please explain me clearly step, I will follow and promise to learn more >< thank you very much

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

I would do like this.

``````library(tidyverse)

# make sample data
x0 <- rbind(rep(1,100),rep(NA,25))
x1 <- sample(x0, length(x0) - length(x0) %% 6)
nst <- length(x1) %/% 6
dfm <- tibble(x = x1, st = sort(rep(1:nst, 6)))

# custom function
myave <- function(x){
a <- sum(!is.na(x))
if( a > 2 ){
return(mean(x, na.rm = TRUE))
}else{
return(NA)
}
}

# apply
dfm %>% group_by(st) %>% summarise(vn = myave(x))

``````
1 Like

OMG I don't know who you are but I really want to say thank you very much for spending your time to answer, I couldn't be happy more >//< Thank you, someday I will share this trip to others too!!

oh thank you I never know this words before I am going to do that, Thank you for your advice

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.