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
| # combining the two columns containing states names and codes into one | |
| # This replaces each NA in columns one and two with an empty string. | |
| # This is necessary for me to be able to unite them with tidyr's unite() function | |
| cn.inflow[, 1:2][is.na(cn.inflow[, 1:2])] <- "" | |
| cn.inflow <- unite(cn.inflow, state_name2, 1:2, sep = "") | |
| # This replaces each NA in the last two columns into empty strings | |
| cn.inflow[, 14:15][is.na(cn.inflow[, 14:15])] <- "" | |
| cn.inflow <- unite(cn.inflow, ccode2, 14:15, sep = "") |
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
| split(country.pairs$country, | |
| ceiling(seq_along(country.pairs[, "country"])/n)) | |
| # Credit goes to some user on Stackoverflow, who's name I can't remember. |
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
| # Credit for this function goes to Tony Breyal, who shared it on his blog | |
| # https://tonybreyal.wordpress.com/2011/11/24/source_https-sourcing-an-r-script-from-github/ | |
| source_https <- function(url, ...) { | |
| # load package | |
| require(RCurl) | |
| # parse and evaluate each .R script | |
| sapply(c(url, ...), function(u) { | |
| eval(parse(text = getURL(u, followlocation = TRUE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))), envir = .GlobalEnv) |
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
| library(plotly) | |
| x <- list(title = "Vote share in previous election") | |
| y <- list(tickmode = "auto", nticks = 2) | |
| p <- plot_ly(MayorElection2, x = L.VoteShareWinner, y = Reelection, | |
| text = paste("Name winner:", NameCandidate1), | |
| mode = "markers", group = SparkassenMember, colors = "Set1", | |
| opacity = 0.5) %>% layout(xaxis = x, yaxis = y) | |
| p |
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
| # selects last character in string | |
| ".{1}$" | |
| # selects all characters after the first expression (content of the first capture group), | |
| # which is a comma in this case | |
| ", (.*)" | |
| # selects newlines or carriage returns | |
| "[\r\n]" |
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
| x <- "foo\nbar\rbaz\r\nquux" | |
| gsub("[\r\n]", "", x) | |
| ## [1] "foobarbazquux" | |
| #Or | |
| library(stringr) | |
| str_replace_all(x, "[\r\n]" , "") |
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
| # removing non-ASCII characters from strings | |
| fin2011[, 1] <- iconv(fin2011[, 1], "utf-8", "ASCII", sub="") | |
| # removing special characters from strings | |
| fin2012[, 2] <- gsub("[[:punct:]]", "", fin2012[, 2]) |