Skip to content

Instantly share code, notes, and snippets.

View chainsawriot's full-sized avatar
💭
Don't fuck up your mental health.

chainsawriot chainsawriot

💭
Don't fuck up your mental health.
View GitHub Profile
@chainsawriot
chainsawriot / rang.R
Last active February 16, 2023 16:27
require(rang)
require(purrr)
require(igraph)
convert_edgelist <- function(x) {
output <- data.frame(x = x$pkg, y = rang:::.extract_queryable_dependencies(x$original, x$no_enhances, x$no_suggests))
for (dep in x$deps) {
if (!rang:::.is_terminal_node(dep, x$no_enhances)) {
el <- data.frame(x = unique(dep$x_pkgref), y = rang:::.extract_queryable_dependencies(dep, x$no_enhances, x$no_suggests))
output <- rbind(output, el)
@chainsawriot
chainsawriot / popol.R
Created September 16, 2019 11:42
bimodal
require(tidyverse)
require(tidyr)
require(moments)
nrespondants <- c(223, 41, 63, 86, 64, 251, 82, 73, 74, 17, 64, 168, 43, 47, 64, 35, 135, 28, 15, 43, 10, 43, 366, 45, 73, 50, 26, 121, 25, 38, 44, 5, 46, 260, 36, 54, 49, 31, 63, 26, 30, 29, 3, 44)
score <- rep(0:10, 4)
wave <- sort(rep(1:4, 11))
require(R6)
dog <- list(name = 'hundno', say = 'whhooo!')
class(dog) <- "animal"
cat <- list(name = 'kitty', say = 'meow!')
class(cat) <- "animal"
ipad <- list(name = "iPad mini")
class(ipad) <- "computer"
var click_score_pos = function(rev_id, pos) {
var reviews = document.querySelector("div[role='presentation'].x-grid3-body").querySelectorAll('tr');
tds = reviews[rev_id].querySelectorAll('td')
for (var key of tds.keys()) {
if (tds[key].getAttribute('class').includes('wrong')) {
var exId = key;
}
}
tds[exId + pos].click()
// sleep(2000)
gendf <- function() {
a <- data.frame(x = rnorm(200, 1, 1), y = rnorm(200, 3, 1))
b <- data.frame(x = rnorm(200, 9, 1), y = rnorm(200, 10, 1))
z <- c(rep(1, 200), rep(2, 200))
cbind(rbind(a, b), z)[sample(1:400),]
}
df1 <- gendf()
df2 <- gendf()
@chainsawriot
chainsawriot / rdebugger.R
Last active January 13, 2016 08:40
how to use the R debugger
## prereq:
## 1) How to define a function
## Security level
## Warning: Does not stop execution
## Error: Stop execution
## example of warning
log(-1)
# github.com/chainsawriot
import os, sys
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
# your facebook username and password
USERNAME = ""
@chainsawriot
chainsawriot / analysis.R
Last active August 29, 2015 14:22
rainstorm
require(dplyr)
require(magrittr)
# 95% CI: assume normally distributed
read.table("rainstorm.fwf") %>% mutate(hr = V6 + (V7/60)) %>% group_by(V1) %>% summarise(meanhr = mean(hr), sehr = sqrt(var(hr)/(length(hr)-1)), lowerCI = meanhr - (1.96*sehr), upperCI = meanhr + (1.96*sehr))
# Median and 2.5 and 97.5 percentile. Mean > Median, the data is positive skew and mean will overestimate the central tendency. Use median for robustness
read.table("rainstorm.fwf") %>% mutate(hr = V6 + (V7/60)) %>% group_by(V1) %>% summarise(medianhr = median(hr), lowerqhr = quantile(hr, probs = 0.025), upperqhr = quantile(hr, probs = 0.975))
iris[c(3,2,1),] # index the dataframe by an "indices vector", will only pick the first three rows in reversed order
order(iris$Sepal.Length) # generate an "indices vector" based on the ranked value of Sepal Length
# therefore
iris[order(iris$Sepal.Length, decreasing=TRUE),]
# is ordered by row based on the value of Sepal Length, you still need to specify the column required.
# try these also
@chainsawriot
chainsawriot / gist:8330931
Last active January 2, 2016 16:29
Reading data with multiple comments The comment chars should not be those special regex characters.
### assuming the test.tab have this structure
# hello
# ! This is comment
# # This is also a comment
# 1
# 2
# 3
read.table(text=sub(paste0("[!#]", ".*"), "", readLines("test.tab")), header=TRUE)