Skip to content

Instantly share code, notes, and snippets.

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)
@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(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"))
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)
# 整合预测结果
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)
@xccds
xccds / glmnet.R
Last active November 15, 2020 18:00
library(ggplot2)
library(glmnet)
library(reshape2)
# 读入数据
data <- read.csv('d:/ex2data2.txt',F)
# 散点图
ggplot()+
geom_point(data=data,aes(V1,V2,colour=factor(V3),
shape=factor(V3)),size=3)
\documentclass[UTF8,10pt]{ctexart}
\usepackage[a4paper,%%textwidth=129mm,textheight=185mm, %%193-8
text={160mm,260mm},centering]{geometry}
\pagestyle{empty}
\begin{document}
\title{用散点图示范ggplot2的核心概念}
\author{肖凯}
\maketitle
\abstract{
本文稿是第五届R语言会议演讲内容的一部分,试图用散点图示例来说明ggplot2包的核心概念,以方便初学者快速上手。同时这也是笔者应用knitr包的一个练习。该示例所用数据是ggplot2包内带的mpg数据集。}
# 导入数据
text <- readLines('d:\\honglou.txt',encoding='UTF-8')
library(ggplot2)
library(rmmseg4j)
library(tm)
library(MASS)
library(proxy)
#去除空白行
library(twitteR)
library(plyr)
library(ggplot2)
# 抓取北京和上海空气数据的推文
airb <- userTimeline("beijingair", n=660)
airs <- userTimeline("CGShanghaiAir", n=660)
airg <- userTimeline("Guangzhou_Air", n=660)
#提取文本后用正则表达式分割
# 读取数据
raw <- read.csv('http://www.stat.yale.edu/~jay/EPI_data_download/EPI_2012_Final_Results.csv',T)
names(raw)
data <- raw[,c(2,7,8,10,23:44)]
data <- data[!is.na(data$GDPgroup),]
library(reshape)
# 数据整理
data.melt <- melt(data,id=c('Country','GDPCAP','GDPgroup'))
data.melt.china <- data.melt[data.melt$Country=='China',]
data.melt.china$variable <- with(data.melt.china,