Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hughjonesd/namer
Manipulate objects by their names
https://github.com/hughjonesd/namer
Last synced: 25 days ago
JSON representation
Manipulate objects by their names
- Host: GitHub
- URL: https://github.com/hughjonesd/namer
- Owner: hughjonesd
- License: other
- Created: 2021-03-06T16:27:11.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-07-04T10:29:07.000Z (4 months ago)
- Last Synced: 2024-10-09T09:39:02.521Z (about 1 month ago)
- Language: R
- Size: 117 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - hughjonesd/namer - Manipulate objects by their names (R)
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```[![R-CMD-check](https://github.com/hughjonesd/namer/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/hughjonesd/namer/actions/workflows/R-CMD-check.yaml)
`{namer}` is a tiny r package containing convenience functions for manipulating
objects by their names. Using these functions makes your code easier to read,
and reduces duplication:```{r}
library(namer)vec <- c(One = 1, Two = 2, Three = 3, Four = 4)
# Base R:
vec[startsWith(names(vec), "T")]# Clearer:
vec |> named_starting("T")# Base R:
some_names <- names(vec) %in% c("Two", "Three")
names(vec)[some_names] <- tolower(names(vec)[some_names])# Clearer:
vec |> rename_in(c("Two", "Three"), tolower)# Base R:
vec[sort(names(vec))]# Clearer:
vec |> sort_by_name()```
Functions that start with `named` return a subset of the original
object:```{r}
vec <- c(One = 1, Two = 2, Three = 3, Four = 4)
vec |> named_in(c("Two", "Three", "Non-existent"))
vec |> named_starting("T")
vec |> named_like("[A-Z].*e$")
````sort_by_name()` sorts object by name:
```{r}
sort_by_name(vec)
```Functions that start with `rename` return the object with its
names changed. You can use a named character vector:```{r}
vec |> rename_in(c("One", "Two"), c(one = "One", two = "Two"))
```Or an unnamed character vector:
```{r}
vec |> rename_in(c("One", "Two"), c("First", "Second"))
```Or a function:
```{r}
vec |> rename_all(tolower)
vec |> rename_starting("T", tolower)
```Or you can use a one-sided formula, as in [purrr](https://purrr.tidyverse.org/):
```{r}
vec |> rename_in(c("One", "Two"), ~paste(.x, 1:2, sep = "."))
```Or use a regular expression with `rename_gsub`:
```{r}
vec |> rename_gsub("[aeiou]", "e")
```Or match names from old to new with `rename_lookup`:
```{r}
df <- data.frame(
old = c("One", "Two", "Three", "Four"),
new = c("A", "B", "C", "D")
)
vec |> rename_lookup(df$old, df$new)
```## Installation
You can install from R-universe:
``` r
install.packages("namer", repos = c("https://hughjonesd.r-universe.dev",
"https://cloud.r-project.org"))
```Or install the development version from [GitHub](https://github.com/):
``` r
# install.packages("remotes")
remotes::install_github("hughjonesd/namer")
```