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.
Thanks already in advance !
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
}