-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
892 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
^Meta$ | ||
^doc$ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
SupplementaryMaterials | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
Meta | ||
doc | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.Ruserdata | ||
SupplementaryMaterials | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
Package: comprehenr | ||
Type: Package | ||
Title: What the Package Does (Title Case) | ||
Version: 0.1.0 | ||
Author: Who wrote it | ||
Maintainer: The package maintainer <[email protected]> | ||
Description: More about what it does (maybe more than one line) | ||
Use four spaces when indenting paragraphs within the Description. | ||
License: What license is it under? | ||
Title: List comprehensions in R | ||
Version: 0.5.0 | ||
Maintainer: Gregory Demin <[email protected]> | ||
Authors@R: person("Gregory", "Demin", email = "[email protected]", | ||
role = c("aut", "cre")) | ||
Description: Package provides 'Python'-style list comprehensions for R. | ||
It uses ususal R loops ('for', 'while' and 'repeat') and usual 'if' as list producers. | ||
Simple example: 'to_list(for(i in 1:10) if(i %% 2==0) i*i)'. | ||
URL: https://github.com/gdemin/comprehenr | ||
BugReports: https://github.com/gdemin/comprehenr/issues | ||
Depends: | ||
R (>= 3.3.0), | ||
Suggests: | ||
knitr, | ||
testthat, | ||
VignetteBuilder: knitr | ||
License: GPL-2 | ||
Encoding: UTF-8 | ||
LazyData: true | ||
RoxygenNote: 6.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
exportPattern("^[[:alpha:]]+") | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(enumerate) | ||
export(mark) | ||
export(numerate) | ||
export(to_list) | ||
export(to_vec) | ||
export(unmark) | ||
export(unnumerate) | ||
export(unzip_list) | ||
export(zip_lists) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0.5 (19.02.2019) | ||
================ | ||
* Initial release |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#' Auxiliary functions for working with lists | ||
#' | ||
#' \itemize{ | ||
#' \item{numerate}{ returns list of lists. Each list consists of two elements: | ||
#' sequential number of element and element. Reverse operation - | ||
#' \code{unumerate}.} | ||
#' \item{mark}{ returns list of lists. Each list consists of two elements: name of element and element. Reverse operation - | ||
#' \code{unmark}.} | ||
#' \item{zip_lists}{ combine lists side-by-sidy. Reverse operation - \code{unzip_list}.} | ||
#' \item{unzip_list}{ It's similair to matrix transposition but for list of lists.} | ||
#' } | ||
#' | ||
#' @param x list or list of lists | ||
#' @param item numeric number of list in which stored values | ||
#' @param ... lists which will be zipped | ||
#' | ||
#' @return list or list of lists | ||
#' @export | ||
#' | ||
#' @examples | ||
#' cities = c('Chicago', 'Detroit', 'Atlanta') | ||
#' airports = c('ORD', 'DTW', 'ATL') | ||
#' pairs = zip_lists(cities, airports) | ||
#' | ||
#' str(pairs) | ||
#' str(unzip_list(pairs)) | ||
#' | ||
#' str(numerate(cities)) | ||
#' | ||
#' named_list = c('Chicago' = 'ORD', 'Detroit' = 'DTW', 'Atlanta' = 'ATL') | ||
#' str(mark(named_list)) | ||
#' | ||
numerate = function(x){ | ||
unzip_list(list(seq_along(x), x)) | ||
} | ||
|
||
#' @export | ||
#' @rdname numerate | ||
enumerate = numerate | ||
|
||
#' @export | ||
#' @rdname numerate | ||
unnumerate = function(x, item = 2){ | ||
unzip_list(x)[[item]] | ||
} | ||
|
||
#' @export | ||
#' @rdname numerate | ||
mark = function(x){ | ||
all_names = names(x) | ||
if(is.null(all_names)) all_names = rep("", length(x)) | ||
unzip_list(list(all_names, x)) | ||
|
||
} | ||
|
||
|
||
#' @export | ||
#' @rdname numerate | ||
unmark = function(x, item = 2){ | ||
res = unzip_list(x) | ||
set_names( | ||
res[[item]], | ||
unlist(res[[1]], use.names = FALSE) | ||
) | ||
} | ||
|
||
|
||
#' @export | ||
#' @rdname numerate | ||
unzip_list = function(x){ | ||
all_lengths = unique(lengths(x)) | ||
if(length(x)==0) return(x) | ||
if(length(all_lengths)!=1){ | ||
stop("each element of the argument 'x' should have the same length.") | ||
} | ||
global_names = names(x) | ||
local_names = lapply(x, names) | ||
get_names = function(i){ | ||
res = lapply(local_names, "[[", i) | ||
all_lengths = lengths(res) | ||
if(all(all_lengths==0)) { | ||
return(global_names) | ||
} | ||
res[all_lengths==0] = "" | ||
unlist(res, use.names = FALSE) | ||
|
||
} | ||
lapply(seq_along(x[[1]]), function(i) set_names(lapply(x, `[[`, i), get_names(i))) | ||
} | ||
|
||
#' @export | ||
#' @rdname numerate | ||
zip_lists = function(...){ | ||
unzip_list(list(...)) | ||
} | ||
|
||
|
||
set_names = function (object = nm, nm) { | ||
names(object) = nm | ||
object | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# List comprehensions in R | ||
|
||
Package provides 'Python'-style list comprehensions for R. It uses ususal R loops (`for`, `while` and `repeat`) and usual `if` as list producers. Syntax is very similiar to Python. The diffrenece is that returned value shoud be at the end of the loop body. | ||
|
||
```R | ||
# rather useless statement - squares of even numbers | ||
to_list(for(i in 1:10) if(i %% 2==0) i*i) | ||
|
||
# Pythagorean triples | ||
to_list(for (x in 1:20) for (y in x:20) for (z in y:20) if (x^2 + y^2 == z^2) c(x, y, z)) | ||
|
||
colours = c("red", "green", "yellow", "blue") | ||
things = c("house", "car", "tree") | ||
to_vec(for(x in colours) for(y in things) paste(x, y)) | ||
|
||
# prime numbers | ||
noprimes = to_vec(for (i in 2:7) for (j in seq(i*2, 99, i)) j) | ||
primes = to_vec(for (x in 2:99) if(!x %in% noprimes) x) | ||
primes | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.