Code Monkey home page Code Monkey logo

useless_r_functions's Introduction

Useful - Useless R functions - UUR

UUR is useful, yet useless set of custom R functions using {base} for your everyday R use. That's it :-) And nothing more.

Feature Summary

Cloning the repository

You can follow the steps below to clone the repository.

git clone https://github.com/tomaztk/Useless_R_functions.git

Quickstart

  1. Clone the repository
  2. Start using any of the functions e.g. IsItFriday13()
  3. Explore and enjoy!
# non-deterministic
IsItFriday13 <- function(){
  #da <- "2020-11-13"
  da <- Sys.Date()
  rn <- as.POSIXlt(da)$wday
  d <- as.POSIXlt(da)$mday 
  
  if (rn == 5 & d == 13){
      print("It is a Friday the 13th!")
  } else {
      print("Not a Friday 13th!")
    }
  }  

IsItFriday13()

Contributors and co-authors

Thanks to these wonderful community people making and helping this useless R functions better. Contributions of any kind is highly appreciated!

Riccardo Porreca
Riccardo Porreca

retooooo
retooooo

mrdwab
mrdwab

TFellet
TFellet

rachelostic
rachelostic

Contact

Feel free to get in touch for new useful-useless functions.

Gallery (from blog)

Vignettes

General examples

ToDo

  • Possibly change some functionalities to work properly on UNIX / WIN machines
    • x11() and CLI export
  • A vignette finished; write proper feature details!
  • Create useless R package.
  • Add Documentation for each function.

useless_r_functions's People

Contributors

rachelostic avatar retooooo avatar riccardoporreca avatar tomaztk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

useless_r_functions's Issues

Alternative approach to WordScrambler

Hi Tomaž. I know the typical idiom for scrambling letters in R involves strsplit.

Here's another approach: use charToRaw and rawToChar instead.

# Helper function. Can also be placed inside of WordScrambler() instead or be an
# anonymous function in the `tapply` step.
fun <- function(x) rawToChar(sample(x))

WordScrambler <- function(text) {
    w <- charToRaw(text)
    words <- cumsum(w == "20")
    tolower(paste(tapply(w[w != "20"], words[w != "20"], fun), collapse = " "))
}

calc refinements

Hi. I'm assuming that the goal of this exercise is about constructing a function programmatically, not the output. If it were just the output, I'm sure that you'd just want something like:

calc2 <- function(a, b, oper) sprintf('Result is %g', match.fun(oper)(a, b))

But looking at your approach below:

df <- data.frame(merge(merge(c(1:10), c(1:10), by=NULL), c("+","-","/","*"), by=NULL))
colnames(df) <- c("numberA", "numberB", "oper")
f <- "calc <- function(a,b,oper){"
for (i in 1:nrow(df)){
res <- paste0(as.character(df$numberA[i]) , df$oper[i], as.character(df$numberB[i]))
rr <- eval(parse(text=res))
f1 <- paste0(' if(a==',as.character(df$numberA[i]), ' & b==', as.character(df$numberB[i]), ' & oper==', '"',as.character(df$oper[i]),'"' ,
'){print("Result is ', as.character(rr),'")}', '\n' , collapse=NULL)
f <<- paste0(f, f1, collapse = NULL)
if(i==nrow(df)){
f <<- paste0(f, "}", collapse = NULL)
eval(parse(text=f))
}
}

I think it would be easier to ditch the loops and use sprintf for a lot of the function creation instead. Something like:

df <- expand.grid(numA = 1:10, oper = c("+", "-", "/", "*"), numB = 1:10, stringsAsFactors = FALSE)
rr <- sapply(1:nrow(df), function(x) match.fun(df[x, "oper"])(df[x, "numA"], df[x, "numB"]))
template <- ' if (a == %s & b == %s & oper == "%s") print("Result is %g")\n '
f <- sprintf("calc <- function(a, b, oper) {\n%s\n}", 
             paste0(with(df, sprintf(template, numA, numB, oper, rr)), collapse = ""))
eval(parse(text = f))

Changes were:

  • expand.grid to create the "df"
  • sapply to pre-calculate the results
  • sprintf to piece together the function

canSumMEMO returns incorrect result

Describe the bug
The function canSumMEMO returns incorrect result most of the time.
For example:

canSumMEMO(8, c(5,3)) # returns false should be true
canSumMEMO(8, c(1,5,3)) # returns correctly true
canSumMEMO(8, c(12,5,3)) # return false should be true

The error is probably due to line 57:

# Incorrect version
if (canSumMEMO(remainder, numbers[i], memo) == TRUE) {

# Fixed version to emulate behaviour of canSumBF
if (canSumMEMO(remainder, numbers, memo) == TRUE) {

Timings in the blog post should be impacted as more solutions will be tested by the canSumMEMO function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.