How to find/estimate probability density function from density function in R Suppose that I have a variable like X with unknown distribution. In Mathematica, by using SmoothKernelDensity function we can have an estimated density function.This estimated density function can be used alongside with PDF function to calculate probability density function of a value like X in the form of PDF[density,X] assuming that "density" is the result of SmoothKernelDensity. It would be good if there is such feature in R.This is how it works in Mathematica
http://reference.wolfram.com/mathematica/ref/SmoothKernelDistribution.html
As an example (based on Mathematica functions):
data = RandomVariate[NormalDistribution[], 100]; #generates 100 values from N(0,1)

density= SmoothKernelDistribution[data]; #estimated density

PDF[density, 2.345] returns 0.0588784 

Here you can find more information about PDF:
http://reference.wolfram.com/mathematica/ref/PDF.html
I know that I can plot its density function using density(X) in R and by using ecdf(X) I can obtain its empirical cumulative distribution function.Is it possible to do the same thing in R based on what I described about Mathematica? 
Any help and idea is appreciated.
 A: ?density points out that it uses approx to do linear interpolation already; ?approx points out that approxfun generates a suitable function:
x <- log(rgamma(150,5))
df <- approxfun(density(x))
plot(density(x))
xnew <- c(0.45,1.84,2.3)
points(xnew,df(xnew),col=2)


By use of integrate starting from an appropriate distance below the minimum in the sample (a multiple - say 4 or 5, perhaps - of the bandwidth used in df would generally do for an appropriate distance), one can obtain a good approximation of the cdf corresponding to df.
A: spatstat.core::CDF() can be used to to create a cumulative density function from a given output from density().
set.seed(123)
x <- rnorm(10000000)

x_density <- density(x, n = 10000)

x_cdf <- spatstat.core::CDF(x_density)

sds <- c(-2, -1, 0, 1, 2)
names(sds) <- sds

# check cdf at different values
setNames(
  x_cdf(sds), 
  sds)
#>         -2         -1          0          1          2 
#> 0.02285086 0.15889356 0.50009332 0.84134448 0.97717762

# compare against theoretical
pnorm(sds)
#>         -2         -1          0          1          2 
#> 0.02275013 0.15865525 0.50000000 0.84134475 0.97724987

Created on 2021-11-22 by the reprex package (v2.0.0)
Update
A previous version of this answer copied code from the deprecated spatstat:::CDF() which was broken up (in ?2020?) into several other packages. If anyone knows a lighter weight package where this CDF function currently exists would love to hear about it in the comments!
