https://github.com/ingmarboeschen/mongotable
Generates one-dimensional and two-dimensional frequency tables from data stored in a MongoDB database.
https://github.com/ingmarboeschen/mongotable
frequency-table mongodb r
Last synced: about 1 month ago
JSON representation
Generates one-dimensional and two-dimensional frequency tables from data stored in a MongoDB database.
- Host: GitHub
- URL: https://github.com/ingmarboeschen/mongotable
- Owner: ingmarboeschen
- License: gpl-3.0
- Created: 2024-11-11T09:20:44.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-10T07:48:23.000Z (about 1 year ago)
- Last Synced: 2025-10-12T22:49:12.704Z (9 months ago)
- Topics: frequency-table, mongodb, r
- Language: R
- Homepage:
- Size: 253 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoTable::mongoTable()
This R function generates one-dimensional and two-dimensional frequency tables from data stored in a MongoDB database. Rather than retrieving the entire dataset through a `find()` operation, this function leverages MongoDB's aggregation capabilities to compile frequency data directly on the server, followed by post-processing in R.
## Usage
```R
mongoTable(connection, x, y = NULL, query = "{}",lowerize = FALSE, limit = NULL, sort = FALSE, decreasing = TRUE)
```
## Arguments
| Name | Description|
| :--- | :--- |
| connection | character. A mongo connection object initiated with mongolite::mongo().|
| x | character. A field variable for which frequencies should be counted.|
| y | character. An optional second field variable for which frequencies should be counted.|
| query | character. An optional MongoDB query for data subset selection (e.g.: ’{\"year\": 2024}’). |
| lowerize | logical. All levels in one dimensional tables will be lowerized. |
| limit | integer. Defines the maximum length/dimensions of output.|
| sort | logical. If TRUE, the output is sorted by frequency.|
| decreasing | logical. If TRUE and sort==TRUE, the output is returned with decreasing frequencies. If TRUE and sort==FALSE, level names are returned in decreasing manner.|
## Installation with the pak package
```R
if(require(pak)!=TRUE) install.packages("pak")
pak::pak("ingmarboeschen/mongoTable")
```
## Examples
```R
## use mongolite::mongo() to connect to a MongoDB instance (demo server)
mon <- mongolite::mongo("mtcars", url = "mongodb+srv://readwrite:test@cluster0-84vdt.mongodb.net/test")
if(mon$count() > 0) mon$drop()
mon$insert(mtcars)
stopifnot(mon$count() == nrow(mtcars))
## Create a one-dimensional frequency table
# for all x
mongoTable(connection = "mon", x = "cyl")
# for all x matching a query (cars with mpg greater than 20)
mongoTable(connection="mon", x="cyl", query = '{\"mpg\": {\"$gt": 20}}')
## Create a two-dimensional frequency table
# for all x and y
mongoTable(con = "mon", x = "cyl", y = "gear")
# for all x and y matching a query (cars with mpg greater than 20)
mongoTable(con="mon", x = "cyl", y = "gear", query = '{\"mpg\": {\"$gt": 20}}')
```