R語言學習資源,r語言學習
入門視頻教程
R語言初級課程(1)- R語言快速入門http://cos.name/videos/intro-2-r/
代碼
#對象1+1*3c(1,2,3,4,5)c('helloworld','i am a R user')c("hehe","haha")1:66:1exp(1:4)log(1:3)a<-c(1,2,3,4,5)a[1]a[1:3]a[-4]a>3a[a>3]#數組,類型需要一致x<-1:12a<-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)peoplepeople[2,3]people[,2]people$agepeople$age>30people[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 sex1 aa 12 F2 bb 34 M3 cc 45 F4 dd 67 M5 ee 78 F> people[2,3][1] MLevels: 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 sex2 bb 34 M3 cc 45 F4 dd 67 M5 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,右邊表示xplot(y~x)#預設離散點plot(y~x,type='l')#lineplot(y~x,type='h')#histhist(y)#長條圖#使用lattice包library(lattice)num<-sample(1:3,size=50,replace=1)barchart(table(num))stripplotdensityplotxyplothistgram#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:10each(min,max)(x)colwise(mean,is.numeric)(iris)
R語言初級課程(5)- 迴歸分析http://cos.name/videos/r101-regression/