Skip to content

Instantly share code, notes, and snippets.

View Nicktz's full-sized avatar

Nico Katzke Nicktz

View GitHub Profile
@Nicktz
Nicktz / gist:8404fc914c9886ca5614
Created November 23, 2015 13:02
Using lapply to import a list of csv's using read_csv
The following imports a long list of csv's fast using lapply and read_csv:
# File list:
data.files <- list.files(production.dataset(Universe,"EQS"),pattern = "*.csv",full.names=TRUE, recursive = FALSE, include.dirs = FALSE)
# Import all the files as separate lists
df.eqs <- lapply( data.files, function(x) read_csv(x))
# Append the files by row:
x <- df.eqs[[1]]
@Nicktz
Nicktz / gist:147d23fe6eb43874c44f
Created October 29, 2015 10:43
Installing plotly - Fairtreegists
Following [this](https://plot.ly/r/getting-started/) howto, go [here](https://plot.ly/) and sign up using github.
Once this has been done, take not of your username (same as github) and api key.
Then go to R and use the following code.
install.packages("viridis") # dependency
install.packages("devtools")
devtools::install_github("ropensci/plotly")
library(plotly)
@Nicktz
Nicktz / gist:aa1d771dcac92f7b10d5
Created October 29, 2015 09:39
Using GSUB - Fairtreegists
We uuse GSUB to change parts of a string.
This is particularly useful if we want to replace parts of a word or phrase.
It can be used as follows to, e.g., replace all the '/' by a '_' in data:
data[y,z]<-gsub("/","_", data[y,z])
To remove all parts of a string after a sign, we can also use gsub.
E.g., if we have naming conventions such as:
devtools::install_github("hadley/ggplot2")
@Nicktz
Nicktz / gist:080a1a6ed285828dc9e5
Created January 6, 2016 09:22
Footnote to ggplot
http://statmodeling.com/best-way-to-add-a-footnote-to-a-plot-created-with-ggplot2.html
@Nicktz
Nicktz / gist:de78937b33697aadd70f
Created January 19, 2016 06:49
Automation of r script execution
R can execute scripts automaticall and at specific times.
See [this](http://www.r-bloggers.com/scheduling-r-markdown-reports-via-email/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+RBloggers+%28R+bloggers%29) to see how to generate automated r scripts and emails with Rmarkdowns.
Adjusting the documentation of ?suppressWarning to only suppress a very specific warning, e.g. if we know a function will warn about zero standard deviation:
foo <- function() {
x <- cor( c(1,1,1,1,1),c(1:5),method = 'spearman',use = "pairwise.complete.obs")
}
x <- foo() # This produces the warning: "the standard deviation is zero"
To suppress this warning when running foo(), wrap it with the following function:
SetNames allows a mutate function to design a formula and rename the new mutated column accordingly. It works perfectly well across vectors as well.
Another benefit is that setNames can then also be run to check the formula:
E.g. to create a % column for Variable:
Variable <- "FactorX"
DF %>%
mutate_(.dots =
setNames( paste0(Variable,"/sum(",Variable,", na.rm = TRUE))"), paste0(Variable, "Pct"))
If a new package needs to be included into the knitr defaults, use the following process:
Download the package's .tar file and drop it into: (either download this, or find it in: C:\Program Files (x86)\MiKTeX 2.9\source after installing it using programs/miktex/packagemanager)
C:\Program Files\Microsoft\MRO\R-3.2.3\library\knitr\misc
A large number of Tarfiles are located here: https://www.ctan.org/tex-archive/systems/win32/miktex/tm/packages?lang=en
To find your knitr folder, type in R: path.package()
Now add the \usepackage command to the default tex file and it can now be used inside rmarkdown by calling the function as follows (for e.g. adjumulticol package):
This is a useful means of using vector inputs in filters for dplyr:
Function_Name <- lazyeval::interp(~Function(a), a = as.name(VectorInput))
df %>%
filter_(Function_Name)
This applies the Function on the VectorInput to a filter.
E.g., to get complete cases for a vector, use: