documentr
DESCRIPTION
TRANSCRIPT
![Page 1: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/1.jpg)
R Language
note
![Page 2: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/2.jpg)
Data analysis and graphics with R
• R is a language and environment for statistical computing and graphics, similar to the S language originally developed at Bell Labs
![Page 3: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/3.jpg)
Common commands• getwd() 读工作目录 .• setwd(“c:/”) 改变工作目录• ls() 显示当前 workspace 的变量• rm(objectlist) Remove (delete) one or more objects.• options() View or set current options.• savehistory("myfile") Save the commands history to myfile ( default =• .Rhistory).• loadhistory("myfile") Reload a command's history (default = .Rhistory).• save.image("myfile") Save the workspace to myfile (default = .RData).• save(objectlist,file="myfile") Save specific objects to a file.• load("myfile") Load a workspace into the current session (default =• .RData).
![Page 4: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/4.jpg)
R Data Structures
•
![Page 5: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/5.jpg)
Vector
• Vector– c (2,3,1,6,5,2)– X = c (1:9) – X*2 向量乘• 2,4,6,8,10,12,14,16,18
![Page 6: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/6.jpg)
Array
• array(c(1:9), dim=c(3,3))– [,1] [,2] [,3]– [1,] 1 4 7– [2,] 2 5 8– [3,] 3 6 9
![Page 7: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/7.jpg)
Matrix
myymatrix <- matrix(vector,nrow=number_of_rows, ncol=number_of_columns,byrow=logical_value,dimnames=list(
char_vector_rownames, char_vector_colnames
)
)M = matrix(c(1:10), nrow=2, ncol=5, byrow=T)byrow=T横向填充
[,1] [,2] [,3] [,4] [,5][1,] 1 2 3 4 5[2,] 6 7 8 9 10
byrow=F竖向填充[,1] [,2] [,3] [,4] [,5][1,] 1 3 5 7 9[2,] 2 4 6 8 10
![Page 8: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/8.jpg)
Matrix
a=matrix(c(1:10), nrow=2, ncol=5, byrow=T, dimnames=list(c('R1','R2'),c('a','b','c','d','e')))
a b c d eR1 1 2 3 4 5R2 6 7 8 9 10
• 选择数据:– a[1,2] 结果: 2– a[2,c(3:5)] 结果: 8,9,10
![Page 9: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/9.jpg)
Array
• n1=c('r1','r2')• n2=c('col1','col2','col3')• n3=c('p1','p2','p3','p4')• array(c(1:24),c(2,3,4),dimnames=list(n1,n2,n3)
)三维 数组
![Page 10: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/10.jpg)
data frame
• name=c('jack','tom','joe','linda')• age=c('21','23','20','19')• city=c('bj','sh','sh','bj')• d = data.frame(name,age,city)• Query:
– d['age']– d[c('age','city')]– d[1:2]– d$age
• Edit:– d = edit(d)
![Page 11: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/11.jpg)
data frame
• table(d$city,d$name)– jack joe linda tom– bj 1 0 1 0– sh 0 1 0 1
attach(d)summary(name)…detach(d)
with(d,{summary(name)…})
![Page 12: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/12.jpg)
Lists
• Lists are the most complex of the R data types
• g <- "My First List"• h <- c(25, 26, 18, 39)• j <- matrix(1:10, nrow=5)• k <- c("one", "two", "three")• mylist <- list(title=g, ages=h, j, k)
![Page 13: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/13.jpg)
data input
•
![Page 14: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/14.jpg)
data input
• mydata <- data.frame(age=numeric(0), gender=character(0), weight=numeric(0))
mydata <- edit(mydata)
• mydataframe = read.table("c:/a.txt", header=FALSE, sep="\t", row.names="name")– header: 是否把第一行作为 column name– row.names: 标识
![Page 15: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/15.jpg)
data input
• read excel:– library(RODBC)– channel <- odbcConnectExcel("c:/t.xlsx")– mydataframe <- sqlFetch(channel, "Sheet1")– odbcClose(channel)
• excel2007:– library(xlsx)– workbook <- "c:/myworkbook.xlsx"– mydataframe <- read.xlsx(workbook, 1)
![Page 16: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/16.jpg)
data input
• library(RODBC) • myconn <-odbcConnect("mydsn", uid="Rob",
pwd="aardvark") • crimedat <- sqlFetch(myconn, Crime) • pundat <- sqlQuery(myconn, "select * from
Punishment") • close(myconn)
![Page 17: DocumentR](https://reader035.vdocuments.site/reader035/viewer/2022081907/54c34d2a4a7959782e8b4569/html5/thumbnails/17.jpg)
data export
• png(), jpeg(),• bmp(), tiff(), xfig()• pdf("mygraph.pdf")• …….• dev.off()