|
| 1 | +# About |
| 2 | + |
| 3 | +Vectors can have named elements, which sometimes makes them more convenient to work with. |
| 4 | + |
| 5 | +## Creation |
| 6 | + |
| 7 | +There are three ways to add names to a vector. |
| 8 | + |
| 9 | +1) At vector creation time |
| 10 | + |
| 11 | +```R |
| 12 | +> work_days <- c(Mon = TRUE, Tue = TRUE, Wed = TRUE, Thu = TRUE, Fri = TRUE, Sat = FALSE, Sun = FALSE) |
| 13 | +> work_days |
| 14 | + Mon Tue Wed Thu Fri Sat Sun |
| 15 | + TRUE TRUE TRUE TRUE TRUE FALSE FALSE |
| 16 | +``` |
| 17 | + |
| 18 | +2) By assigning a character vector to [`names()`][ref-names] |
| 19 | + |
| 20 | +```R |
| 21 | +> months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) |
| 22 | +> names(months) <- month.abb |
| 23 | +> months |
| 24 | +Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec |
| 25 | + 31 28 31 30 31 30 31 31 30 31 30 31 |
| 26 | +``` |
| 27 | + |
| 28 | +3) With [`setNames()`][ref-setnames] |
| 29 | + |
| 30 | +```R |
| 31 | +> months <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) |
| 32 | +> setNames(months, month.name) |
| 33 | + January February March April May June July August September October November December |
| 34 | + 31 28 31 30 31 30 31 31 30 31 30 31 |
| 35 | +``` |
| 36 | + |
| 37 | +## Removal |
| 38 | + |
| 39 | +If names are no longer wanted they can be removed by setting to [`NULL`][ref-null] |
| 40 | + |
| 41 | +```R |
| 42 | +> work_days |
| 43 | + Mon Tue Wed Thu Fri Sat Sun |
| 44 | + TRUE TRUE TRUE TRUE TRUE FALSE FALSE |
| 45 | +> names(work_days) <- NULL |
| 46 | +> work_days |
| 47 | +[1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE |
| 48 | +``` |
| 49 | + |
| 50 | +The [`unname()`][ref-unname] function achieves the same thing, and perhaps make your intention clearer. |
| 51 | + |
| 52 | +## Working with names |
| 53 | + |
| 54 | +The [`names()`][ref-names] function can retrieve names as well as set them. |
| 55 | + |
| 56 | +```R |
| 57 | +> names(months) <- month.abb |
| 58 | +> names(months)[1:3] |
| 59 | +[1] "Jan" "Feb" "Mar" |
| 60 | +``` |
| 61 | + |
| 62 | +A name can be used in place of the position index, with quotes required in this case. |
| 63 | + |
| 64 | +```R |
| 65 | +> months[c("Jul", "Aug")] |
| 66 | +Jul Aug |
| 67 | + 31 31 |
| 68 | +``` |
| 69 | + |
| 70 | +For such indexing to work correctly, it is best to ensure that names are unique and non-missing. |
| 71 | +However, R does not enforce uniqueness. |
| 72 | + |
| 73 | +The usual vector operations still work, and names usually will be preserved if that makes sense. |
| 74 | + |
| 75 | +```R |
| 76 | +> months[months == 30] |
| 77 | +Apr Jun Sep Nov |
| 78 | + 30 30 30 30 |
| 79 | + |
| 80 | +> sum(months) |
| 81 | +[1] 365 # no meaningful names possible |
| 82 | +``` |
| 83 | + |
| 84 | +## More advanced topics |
| 85 | + |
| 86 | +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. |
| 87 | + |
| 88 | +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`][concept-lists] and [`dataframes`][concept-dataframes], as we shall see in future concepts. |
| 89 | + |
| 90 | +[ref-names]: https://www.rdocumentation.org/packages/base/versions/3.3.0/topics/names |
| 91 | +[ref-setnames]: https://www.rdocumentation.org/packages/stats/versions/3.5.2/topics/setNames |
| 92 | +[ref-unname]: https://www.rdocumentation.org/packages/base/versions/3.3.0/topics/unname |
| 93 | +[ref-null]: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/NULL |
| 94 | +[concept-lists]: https://exercism.org/tracks/r/concepts/lists |
| 95 | +[concept-dataframes]: https://exercism.org/tracks/r/concepts/dataframes |
0 commit comments