From 21ed2ab70bff18dc043658c56062d0d3348fd98d Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 18 Jul 2023 10:20:54 -0700 Subject: [PATCH] names-attribute concept --- concepts/names-attribute/.meta/config.json | 5 ++ concepts/names-attribute/about.md | 87 ++++++++++++++++++++++ concepts/names-attribute/introduction.md | 81 ++++++++++++++++++++ concepts/names-attribute/links.json | 6 ++ config.json | 5 ++ 5 files changed, 184 insertions(+) create mode 100644 concepts/names-attribute/.meta/config.json create mode 100644 concepts/names-attribute/about.md create mode 100644 concepts/names-attribute/introduction.md create mode 100644 concepts/names-attribute/links.json diff --git a/concepts/names-attribute/.meta/config.json b/concepts/names-attribute/.meta/config.json new file mode 100644 index 00000000..54419de3 --- /dev/null +++ b/concepts/names-attribute/.meta/config.json @@ -0,0 +1,5 @@ +{ + "authors": ["colinleach"], + "contributors": [], + "blurb": "Adding names to vector elements can make them more convenient to work with" +} \ No newline at end of file diff --git a/concepts/names-attribute/about.md b/concepts/names-attribute/about.md new file mode 100644 index 00000000..2dc17b19 --- /dev/null +++ b/concepts/names-attribute/about.md @@ -0,0 +1,87 @@ +# About + +Vectors can have named elements, which sometimes makes them more convenient to work with. + +## Creation + +There are three ways to add names to a vector. + +1) At vector creation time + +```R +> work_days <- c(Mon = TRUE, Tue = TRUE, Wed = TRUE, Thu = TRUE, Fri = TRUE, Sat = FALSE, Sun = FALSE) +> work_days + Mon Tue Wed Thu Fri Sat Sun + TRUE TRUE TRUE TRUE TRUE FALSE FALSE +``` + +2) By assigning a character vector to `names()` + +```R +> months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) +> names(months) <- month.abb +> months +Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + 31 28 31 30 31 30 31 31 30 31 30 31 +``` + +3) With `setNames()` + +```R +> months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) +> setNames(months, month.name) + January February March April May June July August September October November December + 31 28 31 30 31 30 31 31 30 31 30 31 +``` + +## Removal + +If names are no longer wanted they can be removed by setting to `NULL` + +```R +> work_days + Mon Tue Wed Thu Fri Sat Sun + TRUE TRUE TRUE TRUE TRUE FALSE FALSE +> names(work_days) <- NULL +> work_days +[1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE +``` + +The `unname()` function achieves the same thing, and may make your intention clearer. + +## Working with names + +The `names()` function can retrieve names as well as set them. + +```R +> names(months) <- month.abb +> names(months)[1:3] +[1] "Jan" "Feb" "Mar" +``` + +A name can be used in place of the position index, with quotes required in this case. + +```R +> months[c("Jul", "Aug")] +Jul Aug + 31 31 +``` + +For this reason it is best to ensure that names are unique and non-missing, though R does not enforce this. + +The usual vector operations still work, and names usually will be preserved if that makes sense. + +```R +> months[months == 30] +Apr Jun Sep Nov + 30 30 30 30 + +> sum(months) +[1] 365 # no meaningful names possible +``` + +## More advanced topics + +Many R objects, including `vectors`, can have arbitrary attributes attached to them. The `names` attribute is just one example, though a particularly common and important one. + +In practice, names are sometimes useful for vectors but rarely essential. the `names-attribute` becomes much more important for more complex structures such as `lists` and `dataframes`, as we shall see in future concepts. diff --git a/concepts/names-attribute/introduction.md b/concepts/names-attribute/introduction.md new file mode 100644 index 00000000..a90bcbbc --- /dev/null +++ b/concepts/names-attribute/introduction.md @@ -0,0 +1,81 @@ +# Introduction + +Vectors can have named elements, which sometimes makes them more convenient to work with. + +## Creation + +There are three ways to add names to a vector. + +1) At vector creation time + +```R +> work_days <- c(Mon = TRUE, Tue = TRUE, Wed = TRUE, Thu = TRUE, Fri = TRUE, Sat = FALSE, Sun = FALSE) +> work_days + Mon Tue Wed Thu Fri Sat Sun + TRUE TRUE TRUE TRUE TRUE FALSE FALSE +``` + +2) By assigning a character vector to `names()` + +```R +> months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) +> names(months) <- month.abb +> months +Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + 31 28 31 30 31 30 31 31 30 31 30 31 +``` + +3) With `setNames()` + +```R +> months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) +> setNames(months, month.name) + January February March April May June July August September October November December + 31 28 31 30 31 30 31 31 30 31 30 31 +``` + +## Removal + +If names are no longer wanted they can be removed by setting to `NULL` + +```R +> work_days + Mon Tue Wed Thu Fri Sat Sun + TRUE TRUE TRUE TRUE TRUE FALSE FALSE +> names(work_days) <- NULL +> work_days +[1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE +``` + +The `unname()` function achieves the same thing, and may make your intention clearer. + +## Working with names + +The `names()` function can retrieve names as well as set them. + +```R +> names(months) <- month.abb +> names(months)[1:3] +[1] "Jan" "Feb" "Mar" +``` + +A name can be used in place of the position index, with quotes required in this case. + +```R +> months[c("Jul", "Aug")] +Jul Aug + 31 31 +``` + +For this reason it is best to ensure that names are unique and non-missing, though R does not enforce this. + +The usual vector operations still work, and names usually will be preserved if that makes sense. + +```R +> months[months == 30] +Apr Jun Sep Nov + 30 30 30 30 + +> sum(months) +[1] 365 # no meaningful names possible +``` diff --git a/concepts/names-attribute/links.json b/concepts/names-attribute/links.json new file mode 100644 index 00000000..2471228e --- /dev/null +++ b/concepts/names-attribute/links.json @@ -0,0 +1,6 @@ +[ + { + "url": "https://adv-r.hadley.nz/vectors-chap.html#attr-names", + "description": "Advanced R: vector names" + } + ] \ No newline at end of file diff --git a/config.json b/config.json index b51aafcc..36d13c7d 100644 --- a/config.json +++ b/config.json @@ -586,6 +586,11 @@ "uuid": "2751b6f2-7d71-4397-b063-9bf927a57756", "slug": "booleans", "name": "Booleans" + }, + { + "uuid": "be04c596-32c4-497f-9f56-7d939fe7890f", + "slug": "names-attribute", + "name": "Names Attribute" } ], "key_features": [