码迷,mamicode.com
首页 > 编程语言 > 详细

R语言学习资源

时间:2014-12-09 23:11:57      阅读:532      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

入门视频教程

R语言初级课程(1)- R语言快速入门http://cos.name/videos/intro-2-r/

代码

#对象
1+1*3
c(1,2,3,4,5)
c('helloworld','i am a R user')
c("hehe","haha")
1:6
6:1
exp(1:4)
log(1:3)
a<-c(1,2,3,4,5)
a[1]
a[1:3]
a[-4]
a>3
a[a>3]


#数组,类型需要一致
x<-1:12
a<-array(x,c(3,4))
a[2,2]
a[2,]
a[,2]


#数据框(类似数据库表)
city<-c('aa','bb','cc','dd','ee')
age<-c(12,34,45,67,78)
sex<-c('F','M','F','M','F')
people<-data.frame(city,age,sex)
people
people[2,3]
people[,2]
people$age
people$age>30
people[people$age>30,]


#列表,长度可以不一致
mylist<-list(age,city,sex)
mylist


#
class(a)
class(people)
class(mylist)
attributes(people)
str(people)#了解对象详细情况


运行结果

> #对象
> 1+1*3
[1] 4
> c(1,2,3,4,5)
[1] 1 2 3 4 5
> c('helloworld','i am a R user')
[1] "helloworld"    "i am a R user"
> c("hehe","haha")
[1] "hehe" "haha"
> 1:6
[1] 1 2 3 4 5 6
> 6:1
[1] 6 5 4 3 2 1
> exp(1:4)
[1]  2.718282  7.389056 20.085537 54.598150
> log(1:3)
[1] 0.0000000 0.6931472 1.0986123
> a<-c(1,2,3,4,5)
> a[1]
[1] 1
> a[1:3]
[1] 1 2 3
> a[-4]
[1] 1 2 3 5
> a>3
[1] FALSE FALSE FALSE  TRUE  TRUE
> a[a>3]
[1] 4 5
> 
> 
> #数组,类型需要一致
> x<-1:12
> a<-array(x,c(3,4))
> a[2,2]
[1] 5
> a[2,]
[1]  2  5  8 11
> a[,2]
[1] 4 5 6
> 
> 
> #数据框(类似数据库表)
> city<-c('aa','bb','cc','dd','ee')
> age<-c(12,34,45,67,78)
> sex<-c('F','M','F','M','F')
> people<-data.frame(city,age,sex)
> people
  city age sex
1   aa  12   F
2   bb  34   M
3   cc  45   F
4   dd  67   M
5   ee  78   F
> people[2,3]
[1] M
Levels: F M
> people[,2]
[1] 12 34 45 67 78
> people$age
[1] 12 34 45 67 78
> people$age>30
[1] FALSE  TRUE  TRUE  TRUE  TRUE
> people[people$age>30,]
  city age sex
2   bb  34   M
3   cc  45   F
4   dd  67   M
5   ee  78   F
> 
> 
> #列表,长度可以不一致
> mylist<-list(age,city,sex)
> mylist
[[1]]
[1] 12 34 45 67 78

[[2]]
[1] "aa" "bb" "cc" "dd" "ee"

[[3]]
[1] "F" "M" "F" "M" "F"

> 
> 
> #
> class(a)
[1] "matrix"
> class(people)
[1] "data.frame"
> class(mylist)
[1] "list"
> attributes(people)
$names
[1] "city" "age"  "sex" 

$row.names
[1] 1 2 3 4 5

$class
[1] "data.frame"

> str(people)#了解对象详细情况
'data.frame':	5 obs. of  3 variables:
 $ city: Factor w/ 5 levels "aa","bb","cc",..: 1 2 3 4 5
 $ age : num  12 34 45 67 78
 $ sex : Factor w/ 2 levels "F","M": 1 2 1 2 1

R语言初级课程(2)- R语言中的可视化函数http://cos.name/videos/r101-data-visualization-with-r/

代码

#绘图包graphics(基本),lattice(高级),ggplot2(功能强)

#基本绘图函数
x<-c(1,2,3,4,5)
y<-c(2,4,2,4,5)
#左边表示y,右边表示x
plot(y~x)#默认离散点
plot(y~x,type='l')#line
plot(y~x,type='h')#hist
hist(y)#直方图

#使用lattice包
library(lattice)
num<-sample(1:3,size=50,replace=1)
barchart(table(num))

stripplot
densityplot
xyplot
histgram

#lattice 包的三维图形
library(lattice)
wireframe

#ggplot2包
library(ggplot2)
p<-ggplot(...)
print(p)

p<- p+
  stat_smooth()+
  geom_point()+
  scale_color_manual()+
  facet_wrap()+
  opts()+
  labs()



R语言初级课程(3)- R读取数据http://cos.name/videos/r101-data-access/

代码

#控制台的输入
x<-readline()#输入一行
x<-scan()#输入多行

#本地文件输出
output<-file('E:/out.txt')
cat(1:100,sep='\t',file=output)
close(output)


#本地文件输入
output<-file('E:/out.txt')
input<-scan(file=output)
close(output)


#字符串的输入
output<-file('E:/out2.txt')
writeLines(as.character(1:12),con=output)
input<-readLines(output)


#数据表的读写(read.table和write.table)
write.table(iris,file='iris.csv',sep=',')
data<-read.table(file='iris.csv',sep=',')
data<-read.table(file=file.choose(),sep=',')
data<-read.table('clipboard')


#连接数据库
library(RODBC)
channel<-odbcConnect('mysql',uid=user,pwd=password)
sqlTables(channel)
data<-sqlFetch(channel,"customers")
sqlQuery(channel,'select * from orders')



#读excel文档
excelcha<-odbcConnectExcel('c:/iris.xls',readonly=false)
sqlTables(excelcha)
data<-sqlFetch(excelcha,'sheet1')
data$new<-with(data.sqpal_length/sepal_width)
sqlsave(excel,data,tablename='sheet3')


#web数据抓取
library(xml)
url<-'www.google.com/adplanner/static/top1000'
tables<-readHTMLTable(url,stringAsFactors=false,header=F)
data<-tables[[2]]


R语言初级课程(4)- 数据汇总plyr包http://cos.name/videos/r101-plyr/

data(tips,package='reshape2')
library(plyr)
head(tips)
aggregate(x=tips$tip,
          by=list(tip$sex),
          fun=mean)

ddply(data=tips,
      variables='sex',
      fun=function(x){mean(x$tip)})


ratio_fun <-function(x){
  sum(x$tip/sum(x$total_bill))
}


ddply(tips,.(sex),ratio_fun)


x<-1:10
each(min,max)(x)


colwise(mean,is.numeric)(iris)


R语言初级课程(5)- 回归分析http://cos.name/videos/r101-regression/


R语言学习资源

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://blog.csdn.net/zhyoulun/article/details/41044851

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!