Skip to content

Commit 3038175

Browse files
authored
Names attribute (#436)
* Names Attribute concept * add blank line
1 parent 1141c5a commit 3038175

5 files changed

Lines changed: 199 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"authors": [
3+
"colinleach"
4+
],
5+
"contributors": [],
6+
"blurb": "Adding names to vector elements can make them more convenient to work with."
7+
}

concepts/names-attribute/about.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Introduction
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()`
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()`
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`
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()` function achieves the same thing, and may make your intention clearer.
51+
52+
## Working with names
53+
54+
The `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+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://adv-r.hadley.nz/vectors-chap.html#attr-names",
4+
"description": "Advanced R: vector names."
5+
},
6+
{
7+
"url": "https://design.tidyverse.org/names.html",
8+
"description": "Tidyverse design principles."
9+
}
10+
]

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,11 @@
10301030
"uuid": "f6374c07-9244-44b2-b372-3e271b0d01be",
10311031
"slug": "nothingness",
10321032
"name": "Nothingness"
1033+
},
1034+
{
1035+
"uuid": "be04c596-32c4-497f-9f56-7d939fe7890f",
1036+
"slug": "names-attribute",
1037+
"name": "Names Attribute"
10331038
}
10341039
],
10351040
"key_features": [

0 commit comments

Comments
 (0)