library(slidify)
create_deck(“Topic01”, git = TRUE)
# 這是註解,R在執行時會跳過這一行
demo()
指令認識一下R的圖形能力:demo(graphics)
> x <- c(20:25) #冒號表示連續,即從20,21,...,25
> x
## [1] 20 21 22 23 24 25
> x[4] # 第四個數值
## [1] 23
>
> y <- x * 40
> y
## [1] 800 840 880 920 960 1000
> x[1] + y[5]
## [1] 980
> version #查詢自己電腦的系統資訊,注意這指令不必加'()'
## _
## platform i686-redhat-linux-gnu
## arch i686
## os linux-gnu
## system i686, linux-gnu
## status
## major 2
## minor 15.1
## year 2012
## month 06
## day 22
## svn rev 59600
## language R
## version.string R version 2.15.1 (2012-06-22)
## nickname Roasted Marshmallows
> date() #顯示目前時間
## [1] "Fri Aug 17 23:18:54 2012"
getwd() #查詢現在的資料夾
setwd("C:/") #變更資料夾的位置
# 注意:在Windows中,R使用的路徑,斜線是forward slash "/"
加+ 減- 乘* 除/
商數%/% 餘數%%
次方數^
> 60%/%4
## [1] 15
> 25%%5
## [1] 0
> 2^5
## [1] 32
> 3 + 5 * 6 - 7
## [1] 26
> (3 + 5) * (6 - 7)
## [1] -8
> exp(20) #自然指數
## [1] 485165195
> log(1000) #自然對數
## [1] 6.908
> abs(-6) #取絕對值
## [1] 6
> sqrt(100) #開根號
## [1] 10
> round(45.69) #四捨五入
## [1] 46
> ceiling(4.2) #無條件進位
## [1] 5
> floor(55.2) #取比此數小的整數
## [1] 55
> trunc(65.45) #取整數值
## [1] 65
> sort(c(1, 3, 55, 22, 56)) #排序
## [1] 1 3 22 55 56
我們來安裝幾個本課程要用到的套件
install.packages(c("car","gmodels","gdata","Rcmdr"))
update.packages()
update.packages(ask=FALSE) #也可以使用F來代替FALSE;用T來代替TRUE
library(car)
library(gmodels)
# 注意:若無法更新,則應使用管理員最高權限來使用Rstudio和R。
# 也就是在圖示上按右鍵「以管理員身份執行」。
編輯你的工作目錄中的 .Rprofile 檔案,把掛載的指令都寫進去這個檔中。
下次你開啟Rstudio時就會自動掛載這些常用套件了。例如:
file.edit("/你的路徑/.Rprofile")
# 再把以下含有你常用的指令寫入該檔案中,存檔。
library(car)
library(gmodels)
library(Deducer)
ls() 列出物件
objects() 列出物件
rm(x) 清除物件x
rm(list=ls()) 清除所有在暫存區的物件
方式1: attach()
> load("wgcoll.rda")
> table(wgc$g)
##
## 0 1
## 28 22
> attach(wgc)
> table(g)
## g
## 0 1
## 28 22
> detach(wgc)
> table(g)
## Error: object 'g' not found
方式2: with()
> attach(wgc)
> with(wgc, table(g))
## g
## 0 1
## 28 22
> A <- matrix(1:6, nrow = 2, ncol = 3)
> A
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
> B <- matrix(1:6, nrow = 2, byrow = T, dimnames = list(R = c("a",
+ "b"), C = c("c", "d", "e")))
> B
## C
## R c d e
## a 1 2 3
## b 4 5 6
> C <- matrix(c(7, 1, 7, 3, 4, 5, 2, 0, 8), nrow = 3)
> C
## [,1] [,2] [,3]
## [1,] 7 3 2
## [2,] 1 4 0
## [3,] 7 5 8
> a1 <- array(1:24, c(3, 4, 2))
> a1
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 13 16 19 22
## [2,] 14 17 20 23
## [3,] 15 18 21 24
以下三種存取list資料格式的做法很常用,也都適用於list與data frame等資料型態
[[]]
來存取特定變數或欄位[]
來存取特定的變數值$
來取用list中的資料> LIST <- list(表一=seq(2,10,2), 表二= matrix(1:9, nrow=3),
+ 表三=array(1:8, c(2,2,2)), 表四=data.frame(V1=seq(0,3,1)))
> LIST
## $表一
## [1] 2 4 6 8 10
##
## $表二
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
##
## $表三
## , , 1
##
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
##
## , , 2
##
## [,1] [,2]
## [1,] 5 7
## [2,] 6 8
##
##
## $表四
## V1
## 1 0
## 2 1
## 3 2
## 4 3
> LIST$表四
## V1
## 1 0
## 2 1
## 3 2
## 4 3
> DF <- data.frame(V1 = seq(0, 8, 2), V2 = LETTERS[1:5])
> edit(DF)
## V1 V2
## 1 0 A
## 2 2 B
## 3 4 C
## 4 6 D
## 5 8 E
> load("wgcoll.rda")
> class(wgc)
## [1] "data.frame"
> class(wgc$aa)
## [1] "numeric"
> class(wgc$g) #性別是連續變數!?
## [1] "numeric"
> wgc$newg <- factor(wgc$g, levels = 1:2, labels = c("male", "female"))
> class(wgc$newg)
## [1] "factor"
> wgc$g <- as.character(wgc$g) #把數值轉為字元(charact)
> class(wgc$g)
## [1] "character"
> wgc$g <- as.integer(wgc$g) #把字元轉為整數(integers)
> class(wgc$g)
## [1] "integer"
load("/路徑/TDS08L_indQ.rda")
library(foreign)
wgc <- read.csv("/路徑/wgcoll.csv")
library(foreign)
nsysu <- read.spss("/路徑/TEDS08L_indQ.sav",
use.value.labels=FALSE,
to.data.frame=TRUE)
# Windows的電腦請先下載安裝"strawbarry perl" (http://strawberryperl.com)
install.packages("gdata")
library(gdata)
dat <- read.xls("D:\路徑\111110_NSYSU.xls", sheet=1, header=T)
install.packages("sas7bdat")
library(sas7bdat)
sas <- read.sas7bdat("mlogit.sas7bdat")
tail(sas)
sasdata <- read.xport("路徑")
library(foreign)
statadat <- read.dta("http://www.stata-press.com/data/r10/fish.dta")
head(statadat)
sasnew <- as.data.frame(sas)
存成csv檔
write.csv(sasnew,file="sasnew.csv",
sep = ",", row.names = FALSE, col.names = TRUE)
存成R專用的rda檔
save(sasnew, file="sasnew.rda", compress=TRUE)
使用Deducer套件來管理資料和變數:
install.packages("Deducer")
library(Deducer)
指令載入Deducer套件便可以在選單列上看到新增的選單。
deducer()
。?sum
help.search("standard deviation") 最常用的方式
RSiteSearch("standardized coefficients") 搜尋網路資料庫