documentr

17
R Language note

Upload: exsuns

Post on 24-Jan-2015

335 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: DocumentR

R Language

note

Page 2: DocumentR

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

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

R Data Structures

Page 5: DocumentR

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

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

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

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

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

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

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

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

data input

Page 14: DocumentR

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

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

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

data export

• png(), jpeg(),• bmp(), tiff(), xfig()• pdf("mygraph.pdf")• …….• dev.off()