Skip to content

Instantly share code, notes, and snippets.

rm(list = ls())
library(animation)
neighbours <- function(A, i, j) {
# calculate number of neighbours of A[i,j] that are infected
# we have to check for the edge of the grid
nbrs <- 0
# sum across row i - 1
if (i > 1) {
if (j > 1) nbrs <- nbrs + (A[i-1, j-1] == 1)
nbrs <- nbrs + (A[i-1, j] == 1)
library(class)
vknn = function(v,data,cl,k){
# 分割原始数据
grps = cut(1:nrow(data),v,labels=FALSE)[sample(1:nrow(data))]
# 对每份数据分别运行KNN函数
pred = lapply(1:v,function(i,data,cl,k){
omit = which(grps == i)
pcl = knn(data[-omit,],data[omit,],cl[-omit],k=k)
},data,cl,k)
# 整合预测结果
# 加载所需扩展包
library(RCurl)
library(RJSONIO)
require(quantmod)
library(ggplot2)
# 提取武汉市2011年一年的历史数据
date <- seq.Date(from=as.Date('2011-01-01'),
to=as.Date('2011-12-31'), by='1 day')
date.range <- as.character(format(date,"%Y%m%d"))
@xccds
xccds / weather.R
Created April 29, 2012 12:27
get weather data from wunderground
# 加载所需扩展包
library(RCurl)
library(RJSONIO)
library(XML)
# 建立一个根据网址提取天气预报的子函数
fromurl<- function(finalurl) {
# 先读取网页,再解析JSON数据存在raw中
web <- getURL(finalurl)
raw <-fromJSON(web)
high <- raw$forecast$simpleforecast$forecastday[[2]]$high['celsius']
library(plyr)
library(reshape2)
# 三种方式进行数据汇总
data.ddp <- ddply(iris,.(Species),function(df) mean(df[1:4]))
data.agg <- aggregate(iris[1:4],list(iris$Species),mean)
data.melt <- melt(iris,id=c('Species'))
data.dcast <- dcast(data.melt,Species~variable,mean)