How to search repeated sample in data and average them into single value??

df <- arrange(df, date,site,location,collar) 

## how many rows of data do you have?
print(nrow(df))


## add in an identifier to check for repeats 
df$match <- paste(df$date,df$site,df$location,df$collar)

## empty column to take the means of the repeated fluxes
df$meanFlux <- NA


## to all the matches

for (i in 1:length(df$match)){
  if(df$smpl_no[i]>1){
    wch <- which(df[,"match"]==df$match[i])
    df$meanFlux[i] <- mean(df$CO2.flux_qc[wch], na.rm=T)
    df$CO2.flux_qc[wch] <- df$meanFlux[i]
  }
}


##slice sample by group
df %>% group_by(smpl_no) %>% slice (n=3)

## this next bit groups by 'match' and slices the first row, so we end up with a single value for every sampling
## but for repeated samplings this is a mean of all the repeats (if they weren't anyway excluded by qc). 
df <- df %>% group_by(match) %>% 
  slice(1)

You would generally use summarise on a group. If you summarise with mean() you will get the average.

This topic was automatically closed 21 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.