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(dplyr) | |
library(tidyr) | |
library(ggplot2) | |
df <- data_frame(x = rnorm(n = 20, mean = 4), | |
y = rnorm(n = 20, mean = 7)) %>% | |
gather(XY, numbers) | |
ggplot(df, aes(x = XY, y = numbers)) + | |
geom_point(position = position_jitter(width = 0.05)) + |
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(dplyr) | |
d <- data_frame(A =c(rep("a", 5), rep("c",2))) | |
d | |
## one way to do it | |
d %>% | |
group_by(A) %>% | |
filter(length(A) > 2) |
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
has_decimal <- function(x){ (x - floor(x)) > 0 } | |
## from ?is.integer | |
is.wholenumber <- | |
function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol | |
## from http://stackoverflow.com/questions/3476782/how-to-check-if-the-number-is-integer/3477158#3477158 | |
## from SO and also twitter https://twitter.com/polesasunder/status/700091075269005313 | |
is_whole <- function(x) x%%1==0 |
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(rvest) | |
st_tab <- read_html("https://en.wikipedia.org/wiki/List_of_Star_Trek_characters") %>% | |
html_nodes(".wikitable td , .wikitable th") %>% | |
html_text() | |
library(stringr) | |
titles <- st_tab %>% | |
str_detect(regex("^\\n.*\\n$", perl = TRUE)) | |
st_tab[titles] |
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(pageviews) | |
library(dplyr) | |
library(ggplot2) | |
bowie <- article_pageviews(article = "David_Bowie", end = "2016011200") | |
since_dec <- bowie %>% | |
mutate(timestamp = lubridate::ymd_h(timestamp)) %>% | |
filter(timestamp > lubridate::ymd("2015-12-01")) |
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(gapminder) | |
names(gapminder) %>% dput | |
div_mean <- function(x) x/mean(x) | |
myvarnames <- c("pop", "gdpPercap") | |
gapminder2 <- gapminder |
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(rvest) | |
library(magrittr) | |
pubs <- read_html("http://www.scimagojr.com/countryrank.php") %>% | |
html_node(".tabla_datos") %>% | |
html_table(row.names = FALSE) | |
library(tidyr) | |
library(dplyr) |
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
make_plot <- function(mydata){ | |
qplot(Length, Freq, data=mydata) | |
} |
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(rvest) | |
quartet <- html("https://en.wikipedia.org/wiki/Anscombe's_quartet") %>% | |
html_table() %>% | |
.[[2]] | |
for (i in 1:length(names(quartet))) { | |
if (is.na(names(quartet)[i])) { | |
names(quartet)[i] <- names(quartet)[i - 1] | |
} |
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(loggr) | |
library(assertr) | |
library(magrittr) | |
log_file("thelog.log") | |
mtcars %>% | |
insist(within_n_mads(2), mpg:qsec) %>% | |
something |