Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mkearney/attrbl
A tidy approach to attributes
https://github.com/mkearney/attrbl
attr attributes dataframes mkearney-r-package r rstats tibble tidy tidy-data tidyverse
Last synced: 28 days ago
JSON representation
A tidy approach to attributes
- Host: GitHub
- URL: https://github.com/mkearney/attrbl
- Owner: mkearney
- Created: 2018-02-10T20:14:34.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-22T00:06:12.000Z (almost 7 years ago)
- Last Synced: 2024-08-13T07:14:32.696Z (4 months ago)
- Topics: attr, attributes, dataframes, mkearney-r-package, r, rstats, tibble, tidy, tidy-data, tidyverse
- Language: R
- Homepage:
- Size: 23.4 KB
- Stars: 4
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
- jimsghstars - mkearney/attrbl - A tidy approach to attributes (R)
README
---
title: "attrbl"
output: github_document
---```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = FALSE, comment = "#>")
library(attrbl)
library(magrittr)
```A tibble-like package for working with attributes and data frames.
***Warning: this package is in early development***
## Install
Install the dev version from Github
```{r, eval = FALSE}
if (!requireNamespace("devtools", quietly = TRUE)) {
install.packages("devtools")
}
devtools::install_github("mkearney/attrbl")
```## Use case
Let's say you have a list of two data frames. And each data frame contains a data frame attribute "users" with around 100 observations.
```{r, cache=TRUE}
## tweets data with users data attribute
rts <- list(
rtweet::search_tweets("statistics", verbose = FALSE),
rtweet::search_tweets("data science", verbose = FALSE))## each object contains "users" data attribute with around 100 rows
rts %>%
lapply(rtweet::users_data) %>%
lapply(nrow)
```When you bind the data frames using `do.call(..., rbind)`, it returns a "users" attribute. But it only has around 100 rows (it should be closer to 200). It completely **drops** the second "users" attribute!
```{r}
## base R's method
rts %>%
do.call("rbind", .) %>%
attr("users") %>%
nrow()
```When you bind the data frames using `dplyr::bind_rows(...)`, it **doesn't return** a "users" attribute at all!
```{r}
## dplyr's bind_rows method
rts %>%
dplyr::bind_rows() %>%
attr("users")
```But when you bind the data frames using `attrbl::do_call_rbind(...)`, it not only **keeps** all "users" attributes. It binds them together for you!
```{r}
## attrbl's do_call_rbind method
rts %>%
lapply(as_attrbl) %>%
do_call_rbind() %>%
attr("users") %>%
nrow()
```## Usage
### Bind data frames without losing attributes
When a `data.frame` is merged with `dplyr::bind_rows(...)`, it **loses** its attribute(s).
```{r}
## list of data frames
mtcars2 <- list(mtcars, mtcars)## dplyr method
mtcars2 %>%
dplyr::bind_rows() %>%
attr("row.names")
```When an `attrbl` is merged with `attrbl::do_call_rbind`, it **keeps** its attribute(s).
```{r}
## attrbl method
mtcars2 %>%
lapply(as_attrbl) %>%
do_call_rbind() %>%
attr("row.names")
```### Select attributes with `atselect`
Use `atselect` to select attributes the tidy way (no quotes required).
```{r}
atselect(mtcars, row.names, class)
```### Add attributes with `add_attr`
Use `add_attr` to add one or more attributes to a data frame.
```{r}
mtcars %>%
add_attr(seed = quote(set.seed(1234)), timestamp = Sys.time()) %>%
attributes()
```