Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

names-attribute concept #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions concepts/names-attribute/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"authors": ["colinleach"],
"contributors": [],
"blurb": "Adding names to vector elements can make them more convenient to work with"
}
87 changes: 87 additions & 0 deletions concepts/names-attribute/about.md
Original file line number Diff line number Diff line change
@@ -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.
81 changes: 81 additions & 0 deletions concepts/names-attribute/introduction.md
Original file line number Diff line number Diff line change
@@ -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
```
6 changes: 6 additions & 0 deletions concepts/names-attribute/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"url": "https://adv-r.hadley.nz/vectors-chap.html#attr-names",
"description": "Advanced R: vector names"
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down