# std

> For the complete documentation index, see [llms.txt](https://kotlin.github.io/dataframe/llms.txt).

Computes the [standard deviation (std, σ)](https://en.wikipedia.org/wiki/Standard_deviation) of values.

API reference: [std](https://kotlin.github.io/dataframe/api-docs/core/org.jetbrains.kotlinx.dataframe.api/std.html)

`null` values are ignored.

All primitive numeric types are supported: `Byte`, `Short`, `Int`, `Long`, `Float`, and `Double`.

`std` also supports the "mixed" `Number` type, as long as the column consists only of the aforementioned primitive numbers. The numbers are automatically converted to a [common type](numberunification.html) for the operation.

The return type is always `Double`; `Double.NaN` for empty columns.

All operations on `Double`/`Float`/`Number` have the `skipNaN` option, which is set to `false` by default. This means that if a `NaN` is present in the input, it will be propagated to the result. When it's set to `true`, `NaN` values are ignored.

## Delta Degrees of Freedom: DDoF

All `std` operations also have the `ddof` ([Delta Degrees of Freedom](https://en.wikipedia.org/wiki/Degrees_of_freedom_%28statistics%29)) argument.

The default is set to `1`, meaning DataFrame uses [Bessel’s correction](https://en.wikipedia.org/wiki/Bessel%27s_correction) to calculate the "unbiased sample standard deviation" by default. This is also the standard in languages like [R](https://www.r-project.org/). However, it is different from the "population standard deviation" (where `ddof = 0`), which is used in libraries like [Numpy](https://numpy.org/doc/stable/reference/generated/numpy.std.html).

```KOTLIN
df.std() // std of values per every numeric column
df.std { age and weight } // std of all values in `age` and `weight`
df.stdFor(skipNaN = true) { age and weight } // std of values per `age` and `weight` separately, skips NA
df.stdOf { (weight ?: 0) / age } // std of expression evaluated for every row
```

```KOTLIN
df.std()
df.age.std()
df.groupBy { city }.std()
df.pivot { city }.std()
df.pivot { city }.groupBy { name.lastName }.std()
```

See [statistics](summarystatistics.html#groupby-statistics) for details on complex data aggregations.

See [column selectors](columnselectors.html) for how to select the columns for this operation.

## Type Conversion

The following automatic type conversions are performed for the `std` operation:

| Conversion | Result for Empty Input |
| --- | --- |
| Int -> Double | Double.NaN |
| Byte -> Double | Double.NaN |
| Short -> Double | Double.NaN |
| Long -> Double | Double.NaN |
| Double -> Double | Double.NaN |
| Float -> Double | Double.NaN |
| Number -> Conversion([Common number type](numberunification.html)) -> Double | Double.NaN |
| Nothing -> Double | Double.NaN |

## Big numbers

> **Warning:**
> `java.math.BigDecimal` and `java.math.BigInteger` are not supported: `std` throws an exception at runtime for such columns. Compute the standard deviation manually with Kotlin standard library methods and Java big number arithmetic, or [convert](convert.html) the column to a primitive type first.

For a `BigDecimal` column `amount`, the exact standard deviation can be computed with Java `BigDecimal` arithmetic:

```KOTLIN
// exact unbiased sample standard deviation (ddof = 1), computed with Java `BigDecimal` arithmetic
val amounts = df.amount.toList()
val mean = amounts.fold(BigDecimal.ZERO, BigDecimal::add)
    .divide(amounts.size.toBigDecimal(), MathContext.DECIMAL128)
amounts.fold(BigDecimal.ZERO) { acc, value -> acc + (value - mean).pow(2) }
    .divide((amounts.size - 1).toBigDecimal(), MathContext.DECIMAL128)
    .sqrt(MathContext.DECIMAL128)
```

Alternatively, [convert](convert.html) the column to a primitive type first — at the cost of precision:

```KOTLIN
// approximate standard deviation, computed after converting the column to `Double`
df.convert { amount }.toDouble().std { amount }
```

