# Getting Kernel Density Estimates in R Programming – density() Function

`density()` function in R Language is used to compute kernel density estimates.

Syntax: density(x)

Parameters:
x: number vector

Example 1:

 `# R program to illustrate ` `# density function ` ` `  `# Generating 10 numbers randomly ` `x <``-` `stats::rnorm(``10``) ` ` `  `# Getting 10 random number ` `x ` ` `  `# Calling density() function ` `d <``-` `density(x) ` ` `  `# Getting kernel density estimates ` `d `

Output:

```  -0.89243190 -0.76257379  1.17066825  0.09418077  1.08321504 -0.41264708
  0.39009942  0.69991113 -0.82339069 -0.68621705

Call:
density.default(x = x)

Data: x (10 obs.);    Bandwidth 'bw' = 0.4594

x                 y
Min.   :-2.2706   Min.   :0.001546
1st Qu.:-1.0657   1st Qu.:0.047506
Median : 0.1391   Median :0.243764
Mean   : 0.1391   Mean   :0.207189
3rd Qu.: 1.3440   3rd Qu.:0.312477
Max.   : 2.5488   Max.   :0.434704
```

Example 2:

 `# R program to illustrate ` `# density function ` ` `  `# Generating number 1 to 10  ` `x <``-` `seq(``1``, ``10``)  ` `x ` ` `  `# Calling density() function ` `d <``-` `density(x) ` ` `  `# Getting kernel density estimates ` `d `

Output:

```   1  2  3  4  5  6  7  8  9 10

Call:
density.default(x = x)

Data: x (10 obs.);    Bandwidth 'bw' = 1.719

x                 y
Min.   :-4.1579   Min.   :0.0003034
1st Qu.: 0.6711   1st Qu.:0.0092711
Median : 5.5000   Median :0.0538486
Mean   : 5.5000   Mean   :0.0517052
3rd Qu.:10.3289   3rd Qu.:0.0936045
Max.   :15.1579   Max.   :0.0997741
```
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next