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. 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).
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. 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. 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)