From c4b394f597c6a2dd4fdbef8c59cfa8ac8b984cea Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 10 Jul 2023 16:24:22 -0700 Subject: [PATCH] nothingness concept --- concepts/nothingness/.meta/config.json | 5 ++ concepts/nothingness/about.md | 65 ++++++++++++++++++++++++++ concepts/nothingness/introduction.md | 39 ++++++++++++++++ concepts/nothingness/links.json | 10 ++++ config.json | 5 ++ 5 files changed, 124 insertions(+) create mode 100644 concepts/nothingness/.meta/config.json create mode 100644 concepts/nothingness/about.md create mode 100644 concepts/nothingness/introduction.md create mode 100644 concepts/nothingness/links.json diff --git a/concepts/nothingness/.meta/config.json b/concepts/nothingness/.meta/config.json new file mode 100644 index 00000000..17250d42 --- /dev/null +++ b/concepts/nothingness/.meta/config.json @@ -0,0 +1,5 @@ +{ + "authors": ["colinleach"], + "contributors": [], + "blurb": "R uses NULL for complete absence of a value, NA for gaps in our knowledge of the data." +} \ No newline at end of file diff --git a/concepts/nothingness/about.md b/concepts/nothingness/about.md new file mode 100644 index 00000000..b4182065 --- /dev/null +++ b/concepts/nothingness/about.md @@ -0,0 +1,65 @@ +# About + +Many languages have a way such as `null` or `none` to indicate a non-existent value. +Because R is designed to handle large volumes of (often messy) data, it has multiple forms of nothingness. + +The overall aim is to flag missing or suspect values as they are encountered, then continue without raising an exception. + +## NULL + +If a value really doesn't exist, it is repesented by `NULL`. This is probably closest to what C or Python might do. + +```R +> v <- c() # zero-length vector +> v +NULL +> is.null(v) +[1] TRUE +``` + +In many contexts, `NULL` values are simply ignored: + +```R +> c(2, 3, NULL, 5) +[1] 2 3 5 +``` + +## NA + +For situations where a value exists but we don't know what it is, `NA` is used. For example, when counting vehicles traveling on a road, human observers might go off sick or automatic sensors break down, but the traffic continues to flow. + +```R +> v <- c(1, 2, NA, 4, 5) +> v +[1] 1 2 NA 4 5 +> is.na(v) # test for data gaps +[1] FALSE FALSE TRUE FALSE FALSE +``` + +Thus `NA` is a placeholder, warning humans that they need to make a decision about how to handle this. + +Statistical functions usually return `NA` by default if `NA` values are present. +If you want these values to be ignored, use the `na.rm` (NA remove) parameter to make this explicit: + +```R +> v <- c(1, 2, NA, 4, 5) +> mean(v) +[1] NA +> mean(v, na.rm = TRUE) +[1] 3 +``` + +## NaN + +Short for "Not a Number", this flags a computation problem in situations where a number was expected. + +```R +> n <- c(0, 2, 4) +> d <- c(0, 1, 2) +> n / d +[1] NaN 2 2 # 0/0 is mathematically undefined + +``` + +This is not the only possibility for "invalid" calculations. +Dividing by zero will return `Inf` or `-Inf` for a non-zero numerator, without raising an error. diff --git a/concepts/nothingness/introduction.md b/concepts/nothingness/introduction.md new file mode 100644 index 00000000..222abf7b --- /dev/null +++ b/concepts/nothingness/introduction.md @@ -0,0 +1,39 @@ +# Introduction + +Many languages have a way such as `null` or `none` to indicate a non-existent value. +Because R is designed to handle large volumes of (often messy) data, it has multiple forms of nothingness. + +The overall aim is to flag missing or suspect values as they are encountered, then continue without raising an exception. + +## NULL + +If a value really doesn't exist, it is repesented by `NULL`. This is probably closest to what C or Python might do. + +```R +> v <- c() # zero-length vector +> v +NULL +> is.null(v) +[1] TRUE +``` + +In many contexts, `NULL` values are simply ignored: + +```R +> c(2, 3, NULL, 5) +[1] 2 3 5 +``` + +## NA + +For situations where a value exists but we don't know what it is, `NA` is used. For example, when counting vehicles traveling on a road, human observers might go off sick or automatic sensors break down, but the traffic continues to flow. + +```R +> v <- c(1, 2, NA, 4, 5) +> v +[1] 1 2 NA 4 5 +> is.na(v) # test for data gaps +[1] FALSE FALSE TRUE FALSE FALSE +``` + +Thus `NA` is a placeholder, warning humans that they need to make a decision about how to handle this. diff --git a/concepts/nothingness/links.json b/concepts/nothingness/links.json new file mode 100644 index 00000000..56985850 --- /dev/null +++ b/concepts/nothingness/links.json @@ -0,0 +1,10 @@ +[ + { + "url": "http://www.cookbook-r.com/Basics/Working_with_NULL_NA_and_NaN/", + "description": "Cookbook for R" + }, + { + "url": "https://r02pro.github.io/null.html", + "description": "R Programming: Zero to Pro" + } +] \ No newline at end of file diff --git a/config.json b/config.json index 01ec79ac..2c7ef29f 100644 --- a/config.json +++ b/config.json @@ -569,6 +569,11 @@ "uuid": "2751b6f2-7d71-4397-b063-9bf927a57756", "slug": "booleans", "name": "Booleans" + }, + { + "uuid": "f6374c07-9244-44b2-b372-3e271b0d01be", + "slug": "nothingness", + "name": "Nothingness" } ], "key_features": [