# Loop is not calculating the values of Intervals but for the whole df. Help needed

Heey, I’m trying to creat a loop that can calculate a bunch of statistics for a df I have.

The df contains data where each 110 data points (X and Y) have the same time.

I want the loop to calculate these statistics each for all the data points that have the same time but my current loop calculates the statistics for the whole df.

I think my problem right now is, that all the calculations refer to the df as a whole, like

``````
mean(SP\$X)

``````

but when I change it to refere to the Interval it only introduces NAs

``````
mean(Interval\$X)

``````

Here is my current skript. I asume my mistake has something to do with the Interval but I´m not sure how to fix it, maybe somepone can help.

``````
SP <- data

for(i in 1:length(unique(SP\$time))) #loop beginn; use output format (2)

{

Intervall <- SP[(as.numeric(SP\$time))==i,]

attach(Intervall)

mwx <- mean(SP\$X) #Mittelwert

mwy <- mean(SP\$Y)

maxx <- max(SP\$X) #Maximum

maxy <- max(SP\$Y)

minx <- min(SP\$X) #Minimum

miny <- min(SP\$Y)

varx <- var(SP\$X) #Varianz

vary <- var(SP\$Y)

sdx <- sd(SP\$X) #Standardabweichung

sdy <- sd(SP\$Y)

ICVx <- mwx/sdx #Inverser Varianzkoeffizient

ICVy <- mwy/sdy

acx<-acf(SP\$X,lag.max=length(SP\$X),plot=FALSE) #autocorrelation

acy<-acf(SP\$Y,lag.max=length(SP\$Y),plot=FALSE)

psx <-fft(acx\$acf) #Fast Fourier Transformation

psy <-fft(acy\$acf)

psx <-Mod(psx)

psy <-Mod(psy)

psxhalf <-psx[2:((length(psx))/2)]-0.5

psyhalf <-psy[2:((length(psy))/2)]-0.5

psnx <-psxhalf*100/sum(psxhalf)

psny <-psyhalf*100/sum(psyhalf)

psnx <-c(psnx,0)

psny <-c(psny,0)

p <- kIntDur/(1:length(psnx))

wmx <-weighted.mean(p,psnx) #weighted mean per burst

wmy <-weighted.mean(p,psny)

Kx <- kurtosis(SP\$X) #kurtosis per burst

Ky <- kurtosis(SP\$Y)

Sx<-skewness(SP\$X) #skewness per burst

Sy<-skewness(SP\$Y)

q<-sqrt(mwx*mwx+mwy*mwy) #square-root-of-the-sum-of-squares per burst

lda.var<-c(mwx,mwy,maxx,maxy,minx,miny,varx,vary,sdx,sdy,wmx,wmy,ICVx,ICVy,Kx,Ky,Sx,Sy,q)

write(lda.var, file=paste("predictors.txt", sep=","),ncolumns=19,sep=",", append=TRUE)

detach(Intervall) #loop end

}

``````

I think you are making life hard for yourself hand crafting for loops when its not necessary; and there are fairly approachable tools that let one powerfully apply all sorts of functions across columns of a frame very succinctly.

For example consider the mtcars dataset and how one might get min,max,mean values for the columns disp and drat. by `cyl`inder groups

``````library(tidyverse)

mtcars |>
group_by(cyl) |>
summarise(across(where(is.numeric) &
starts_with("d"),
list(avg=mean,
minimum=min,
maximum=max)))
``````

its a few lines of fairly straightforward instructions.
For lessons on how to write code in this vein; see

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