Upcoming SlideShare
Loading in …5
×

# R

605 views

Published on

Published in: Technology, Art & Photos
0 Comments
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

No Downloads
Views
Total views
605
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

### R

1. 1. R Language note
2. 2. 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
3. 3. 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 commands 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).
4. 4. R Data Structures•
5. 5. Vector• Vector – c (2,3,1,6,5,2) – X = c (1:9) – X*2 向量乘 • 2,4,6,8,10,12,14,16,18
6. 6. Array• array(c(1:9), dim=c(3,3)) – [,1] [,2] [,3] – [1,] 1 4 7 – [2,] 2 5 8 – [3,] 3 6 9
7. 7. Matrixmyymatrix <- 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横向填充 byrow=F竖向填充 [,1] [,2] [,3] [,4] [,5] [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [1,] 1 3 5 7 9 [2,] 6 7 8 9 10 [2,] 2 4 6 8 10
8. 8. Matrixa=matrix(c(1:10), nrow=2, ncol=5, byrow=T,dimnames=list(c(R1,R2),c(a,b,c,d,e))) abcd e R1 1 2 3 4 5 R2 6 7 8 9 10• 选择数据： – a[1,2] 结果：2 – a[2,c(3:5)] 结果：8,9,10
9. 9. 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) )三维 数组
10. 10. 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)
11. 11. data frame• table(d\$city,d\$name) – jack joe linda tom – bj 1 0 1 0 – sh 0 1 0 1 attach(d) with(d,{ summary(name) summary(name) … … detach(d) })
12. 12. 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)
13. 13. data input•
14. 14. data input• mydata <- data.frame(age=numeric(0), gender=char acter(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:标识
15. 15. 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)
16. 16. data input• library(RODBC)• myconn <-odbcConnect("mydsn", uid="Rob", pwd="aardvark")• crimedat <- sqlFetch(myconn, Crime)• pundat <- sqlQuery(myconn, "select * from Punishment")• close(myconn)
17. 17. data export• png(), jpeg(),• bmp(), tiff(), xfig()• pdf("mygraph.pdf")• …….• dev.off()