This file contains 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
translit <- function(text, charupper=FALSE){ | |
lat.up <- c("A","B","V","G","D","E","YO","ZH","Z","I","J","K","L","M","N","O", | |
"P","R","S","T","U","F","KH","C","CH","SH","SHH","''","Y","'","E'","YU","YA") | |
rus.up <- c("А","Б","В","Г","Д","Е","Ё","Ж","З","И","Й","К","Л","М","Н","О", | |
"П","Р","С","Т","У","Ф","Х","Ц","Ч","Ш","Щ","Ъ","Ы","Ь","Э","Ю","Я") | |
lat.low <- c("a","b","v","g","d","e","yo","zh","z","i","j","k","l","m","n","o", | |
"p","r","s","t","u","f","kh","c","ch","sh","shh","''","y","'","e'","yu","ya") | |
rus.low <- c("а","б","в","г","д","е","ё","ж","з","и","й","к","л","м","н","о", | |
"п","р","с","т","у","ф","х","ц","ч","ш","щ","ъ","ы","ь","э","ю","я") | |
n <- nchar(text) |
This file contains 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
#Функция отправки почтового сообщения (СМС, если использовать шлюз оператора) | |
#username — имя пользователя почты на gmail.com | |
#password — пароль от почты на gmail.com | |
#emailto — e-mail получателя (например, [email protected] или используя email2sms шлюз оператора 79ХХХХХХХХХ@sms.ycc.ru) | |
#sub — тема сообщения | |
#msg — текст сообщения | |
gmail.send <- function(username, password, emailto, sub, msg) | |
{ | |
frommailuser <- paste(username,"@gmail.com", sep='') |
This file contains 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
# Младенческая смертность в России (2011-2012) и результаты выборов в госдуму (2011) | |
# по регионам РФ | |
# Исходные данные: | |
# - официальные результаты выборов | |
# http://ru.wikipedia.org/wiki/Выборы_в_Государственную_думу_(2011)#cite_note-87 | |
# - данные Росстата по младенческой смертности в 2012 году | |
# http://www.gks.ru/free_doc/2012/demo/t3-3.xls | |
# Загружаем необходимые пакеты | |
library("psych") |
This file contains 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
corstars < - function(x){ | |
require(Hmisc) | |
x <- as.matrix(x) | |
R <- rcorr(x)$r | |
p <- rcorr(x)$P | |
mystars <- ifelse(p < .01, "**|", ifelse(p < .05, "* |", " |")) | |
R <- format(round(cbind(rep(-1.11, ncol(x)), R), 3))[,-1] | |
Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x)) | |
diag(Rnew) <- paste(diag(R), " |", sep="") | |
rownames(Rnew) <- colnames(x) |
This file contains 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
# McCrae's rpa (r profile agreement) | |
rpa <- function(p1,p2){ | |
if (length(p1) != length(p2)) | |
stop("'p1' and 'p2' must have the same length") | |
k <- length(p1) | |
sumM.sq <- sum(((p1 + p2)/2)^2) | |
sumd.sq <- sum((p1 - p2)^2) | |
ipa <- (k + 2*sumM.sq - sumd.sq) / sqrt(10*k) | |
rpa <- ipa / sqrt(k - 2 + ipa^2) | |
return(rpa) |
This file contains 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
#Функция для вычисления Фи-коэффициента Гилфорда для четырехклеточных таблиц | |
#Guilford J. P. The phi-coefficient and chi-square as indices of item validity. — Psychometrika. 1941. VI. P. 11—19. | |
phi.coeff <- function(x){ | |
#Проверка | |
if (!is.matrix(x)) | |
stop("Function only defined for 2-way tables.") | |
a <- x[1,1] | |
b <- x[1,2] | |
c <- x[2,1] |
This file contains 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
# The Gower similarity index | |
gower <- function(p1, p2, max, min){ | |
if (length(p1) != length(p2)) | |
stop("'p1' and 'p2' must have the same length") | |
k <- length(p1) | |
range <- max - min | |
d <- sum(abs(p1 - p2) / range) | |
gs <- 1 - d / k | |
return(gs) | |
} |
This file contains 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
packs <- c("knitr", "ggplot2", "XML", "reshape2", "rCharts", "Cairo") | |
lapply(packs, require, character.only = TRUE) | |
theurl = "http://www.sochi2014.com/medalnyj-zachet" | |
## Grab Data, Clean and Reshape | |
raw <- readHTMLTable(theurl, header=FALSE, | |
colClasses = c(rep("factor", 2), rep("numeric", 4))) | |
raw <- as.data.frame(raw)[, -1] | |
colnames(raw) <- c("Страна", "Золото", "Серебро", "Бронза", "Всего") | |
raw <- with(raw, raw[order(Всего, Золото, Серебро, Бронза), ]) | |
raw <- raw[raw[, "Всего"] != 0, ] |
This file contains 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
read.gspreadsheet <- function(key) { | |
require(RCurl) | |
myCsv <- getURL(paste("https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=", | |
key, "&single=true&gid=0&output=csv", sep = ""), | |
.encoding = "UTF8") | |
read.table(textConnection(myCsv), header = T, sep = ",", stringsAsFactors = FALSE) | |
} |
This file contains 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
google.distance <- function(origin, destination, mode = "driving"){ | |
require(XML) | |
require(RCurl) | |
origin <- gsub(" ","+",origin) | |
destination <- gsub(" ","+",destination) | |
url <- paste0("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=",origin,"&destinations=",destination,"&mode=",mode,"&language=ru_RU") | |
xmlpage <- xmlParse(getURL(url)) | |
duration <- as.numeric(xmlValue(xmlChildren(xpathApply(xmlpage,"//duration")[[1]])$value)) | |
distance <- as.numeric(xmlValue(xmlChildren(xpathApply(xmlpage,"//distance")[[1]])$value)) | |
origin.ad <- xmlValue(xpathApply(xmlpage,"//origin_address")[[1]]) |
OlderNewer