This file contains hidden or 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
| is.R() # Should return TRUE if run in R. |
This file contains hidden or 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
| # Let's say you want to display a table of model coefficients | |
| # in order of their significance, | |
| # and you want to plot the distribution of model residuals, | |
| # but you don't know how to access these values. | |
| # Use str(). | |
| # Generate some random data | |
| NN <- 1000 | |
| theData <- data.frame(Alpha = rnorm(NN), | |
| Beta = rnorm(NN)) |
This file contains hidden or 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
| # Let's say you have an R object that you'd like to share with someone else | |
| # but, for whatever reason, it is necessary to share it in text form. | |
| # Generate a random data.frame | |
| set.seed(1337) | |
| NN <- 10 | |
| theData <- data.frame(Alpha = rnorm(NN), | |
| Beta = rnorm(NN)) | |
| theData$Gamma <- theData$Alpha * 2 + theData$Beta / 2 + rnorm(NN) | |
| print(theData) |
This file contains hidden or 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
| # A method for modifying only select off-diagonal items in a matrix | |
| # From "Thierry" and "Ben Bolker" | |
| # At http://stackoverflow.com/a/11759744/479554 | |
| # A sample matrix | |
| size <- 6 | |
| mat <- matrix(seq_len(size ^ 2), ncol = size) | |
| print(mat) | |
| # A companion matrix that indicates how "off" a diagonal is: |
This file contains hidden or 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
| # Randomly allocating observations into groups, for, e.g. cross-validation | |
| kk <- 10 # Number of partitions, as in "kk-fold cross-validation." | |
| # Here is a data.frame full of good data: | |
| nn <- 1003 | |
| myData <- data.frame(matrix(rnorm(nn * 3), ncol = 3)) | |
| colnames(myData) <- LETTERS[1:3] | |
| # This does not work: | |
| whichK <- sample(LETTERS[1:kk], nrow(myData), replace = T) |
This file contains hidden or 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
| # Alternative to a doubly-nested loop | |
| # Imagine I want to perform an operation on a data frame | |
| # once for each combination of two variables, such as Country and Year | |
| # I can do this with a nested loop, or I can do this with (among other | |
| # things) lapply() | |
| # Generate random data: | |
| allCountries <- LETTERS[1:10] | |
| allYears <- 1990:2012 |
This file contains hidden or 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
| # A simple approach to visually-weighted regression plots | |
| doInstall <- TRUE # Change to FALSE if you don't want packages installed. | |
| toInstall <- c("ggplot2", "reshape2", "MASS") | |
| if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")} | |
| lapply(toInstall, library, character.only = TRUE) | |
| # Generate some data: | |
| nn <- 1000 | |
| myData <- data.frame(X = rnorm(nn), |
This file contains hidden or 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
| doInstall <- TRUE # Change to FALSE if you don't want packages installed. | |
| toInstall <- c("ggplot2", "reshape2", "RColorBrewer") | |
| if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")} | |
| lapply(toInstall, library, character.only = TRUE) | |
| # Generate a random matrix | |
| # This can be any type of numeric matrix, | |
| # though we often see heatmaps of square correlation matrices. | |
| nRow <- 9 | |
| nCol <- 16 |
This file contains hidden or 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
| # https://github.com/wch/ggplot2/wiki/New-theme-system | |
| new_theme_empty <- theme_bw() | |
| new_theme_empty$line <- element_blank() | |
| new_theme_empty$rect <- element_blank() | |
| new_theme_empty$strip.text <- element_blank() | |
| new_theme_empty$axis.text <- element_blank() | |
| new_theme_empty$plot.title <- element_blank() | |
| new_theme_empty$axis.title <- element_blank() | |
| new_theme_empty$plot.margin <- structure(c(0, 0, -1, -1), unit = "lines", valid.unit = 3L, class = "unit") |
This file contains hidden or 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
| # Transforming a color scale | |
| doInstall <- TRUE # Change to FALSE if you don't want packages installed. | |
| toInstall <- c("ggplot2", "RColorBrewer", "scales") | |
| if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")} | |
| lapply(toInstall, library, character.only = TRUE) | |
| # Generate some data | |
| nn <- 10000 | |
| myData <- data.frame(X = rnorm(nn), |