Try this... For your original 4800 length dataset it takes 2.5 seconds.
KDens2 = function(x,h,N) {
Kx <- outer( x , x , FUN = function(x,y) dnorm( ( x-y ) / h ) / h )
fx <- as.matrix( rowSums( Kx ) / N , ncol = 1 )
return( fx )
}
Testing
set.seed(1)
trywiththis <- rnorm(480)
x = trywiththis
N = length(trywiththis)
h = 1.059*sd(trywiththis , na.rm=T)*(N^(-0.2))
#Produce same result? (posibly not identical because of 'dnorm' function)
all.equal( KDens(x,h,N) , KDens2(x,h,N) )
[1] TRUE
#Rough timing on N=480 length data...
system.time( KDens2(x,h,N) )
# user system elapsed
# 0.01 0.00 0.02
system.time( KDens(x,h,N) )
# user system elapsed
# 2.7 0.0 2.7
And when N=4800
...
system.time( KDens2(x,h,N) )
user system elapsed
2.33 0.19 2.51