R language learning resources, r Language Learning
Getting started video tutorial
R language beginner course (1)-R language Quick Start http://cos.name/videos/intro-2-r/
Code
# Object 1 + 1 * 3c (1, 2, 3, 4, 5) c ('helloworld', 'I am a R user') c ("hehe", "haha") 1: 66: 1exp () log () a <-c (, 5) a [1] a [] a [-4] a> 3a [a> 3] # array, whose types must be consistent with those of x <-1: 12a <-array (x, c (3, 4) a [2, 2] a [2,] a [, 2] # Data box (similar to a database table) city <-c ('A', '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> 30 people [people $ age> 30,] # list, the length can be inconsistent. mylist <-list (age, city, sex) mylist # class (a) class (people) class (mylist) attributes (people) str (people) # understanding object details
Running result
> # Object> 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] 1 2 3 4 5 6> 6[ 1] 6 5 4 3 2 1> exp) [1] 2.718282 7.389056 20.085537 54.598150> log () [1] 0.0000000 0.6931472 1.0986123> a <-c (, 5)> a [1] [1] 1> a [1: 3] [1] 1 2 3> a [-4] [1] 1 2 3 5> a> 3 [1] false true> a [a> 3] [1] 4 5 >>> # array, type must be consistent> x <-> a <-array (x, c ()> a [] [1] 5> a [2,] [1] 2 5 8 11> a [, 2] [1] 4 5 6 >>># data boxes (similar to database tables)> city <-c ('A ', 'bb ', 'cc', 'dd', 'ee')> age <-c (, 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> people [people $ age> 30,] city age sex2 bb 34 M3 cc 45 F4 dd 67 M5 ee 78 F >># list, the length can be different> 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 () [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) # understand the object details '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 language Elementary Course (2)-visual function http://cos.name/videos/r101-data-visualization-with-r/ in R Language
Code
# Drawing package graphics (basic), lattice (advanced), ggplot2 (strong function) # Basic Drawing Function x <-c (, 5) y <-c, 2, 4, 5) # y on the left and xplot (y ~ X) # default discrete point plot (y ~ X, type = 'l') # lineplot (y ~ X, type = 'H') # histhist (y) # histogram # Use the lattice package library (lattice) num <-sample (, size = 50, replace = 1) barchart (table (num) stripplotdensityplotxyplothistgram # three-dimensional graph library (lattice) wireframe # ggplot2 package library (ggplot2) p <-ggplot (...) print (p) p <-p + stat_smooth () + geom_point () + scale_color_manual () + facet_wrap () + opts () + labs ()
R language Elementary Course (3)-R read data http://cos.name/videos/r101-data-access/
Code
# Console input x <-readline () # input a row x <-scan () # input multiple lines # output local file <-file ('e:/out.txt ') cat (, sep = '\ t', file = output) close (output) # local file input output <-file ('e:/out.txt ') input <-scan (file = output) close (output) # input output of the string <-file ('e:/out2.txt ') writeLines (. character (), con = output) input <', sep =', ') data <-read.table(file='iris.csv', sep = ',') data <-read. table (file = file. choose (), sep = ',') data <-read. table ('clipboard') # connect to the database library (RODBC) channel <-odbcConnect ('mysql', uid = user, pwd = password) sqlTables (channel) data <-sqlFetch (channel, "MERs") sqlQuery (channel, 'select * from orders ') # Read excel document 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 data capture library (xml) url <-'www tables <-readHTMLTable (url, stringAsFactors = false, header = F) data <-tables [[2]
R language Elementary Course (4)-data aggregation plyr package 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 language Elementary Course (5)-regression analysis http://cos.name/videos/r101-regression/