SlideShare una empresa de Scribd logo
1 de 4
##物件命名
y= 2
x+y
x-y
x*y
x/y
x^y
2*x+3*y^2


ls()
rm(x)
ls()
##物件
#numeric, 實數向量
a <- 49
sqrt(a)#sqrt()開根號
mode(a)
b = c(25,36,49)
mode(b)
length(b)
#character string, 文字或字串向量, 通常輸入時, 在文字或字串
兩側加上雙引號.
c <- "my classmate ate my homework"
c sub("classmate","dog",c)#sub(對象, 要取代的名稱,資料)
#logical, 邏輯變數向量 (true or false), 以 TRUE (T) 或
FALSE (F) 呈現,也可以是 1 (T) 與 0 (F)
x <- T; y <- F
z = c(x, y)
z
b
b>30 larger.than.30 = b>30
rbind(b, larger.than.30)#row(列),橫著排
cbind(b, larger.than.30)#column(欄),縱著排 #dataframe,
資料欄(長度要相同)
data.frame(b, larger.than.30)
mydata = data.frame(b, larger.than.30);mydata#使用;在
同一行中執行多個完整的指令
mydata[1,]#顯示第一列
mydata[,2]#顯示第二欄
#使用$(錢字號)指定資料欄中的元件)
mydata$b
mydata$larger.than.30
#使用 attach 綁定資料欄
CO2#R 內建的資料
CO2$uptake
CO2$conc
uptake
attach(CO2)
uptake
conc
test
##讀取資料
#使用 scan()
x1 = scan()
x2 = scan()
scan.failed = scan()
getwd()#查詢目前的工作路徑
read.table("random.csv")
read.table("random.csv",header = TRUE, sep = ",")
random = read.table(file.choose(),
header = TRUE, sep = ",")
str(random)
random random[9,]
random$x
random$y
random$x3#如未填入資料,則 R 讀取資料時會填入 NA
##缺失值 NA, NaN, 與 Null
#NA “not available” x <- c(1, 2, 3, NA)
x+3
#非數字 “Not a number”
log(c(0, 1, 2))
0/0#infinity
Inf - Inf
pi / 0 ## = Inf a non-zero number divided by zero
creates infinity
0 / 0 ## = NaN
1/0 + 1/0 # Inf
1/0 - 1/0 # NaN
#null,物件的長度為 0
 z = c()
z
#任何含有 NA 資料的運算結果都將是 NA
1+NA
max(c(NA, 4, 7))
max(c(NA, 4, 7), na.rm=T)#na.rm,移除缺失值
#回到 random.csv 資料來看
max(random$x3)
max(random$x3,na.rm=T)
mean(random$x3)
mean(random$x3,na.rm=T)
edit(random)#手動編輯資料,進行一些小修改時很有用
random
random.modified = edit(random)
fix(random)#直接修改資料
random
#封面的兔子
require(onion)
data(bunny)
p3d(bunny,theta=3,phi=104,box=FALSE)   	
  

Más contenido relacionado

La actualidad más candente (20)

Ch1 教學
Ch1 教學Ch1 教學
Ch1 教學
 
Ppt 51-77
Ppt 51-77Ppt 51-77
Ppt 51-77
 
Ch2 教學
Ch2 教學Ch2 教學
Ch2 教學
 
P127 135 new
P127 135 newP127 135 new
P127 135 new
 
Appendix A 教學
Appendix A 教學Appendix A 教學
Appendix A 教學
 
Python differential equation
Python differential equationPython differential equation
Python differential equation
 
Ppt 78-100
Ppt 78-100Ppt 78-100
Ppt 78-100
 
Ch3 教學
Ch3 教學Ch3 教學
Ch3 教學
 
Appendix B 教學
Appendix B 教學Appendix B 教學
Appendix B 教學
 
Ch9 範例
Ch9 範例Ch9 範例
Ch9 範例
 
Ch9 教學
Ch9 教學Ch9 教學
Ch9 教學
 
Ch10 範例
Ch10 範例Ch10 範例
Ch10 範例
 
Ppt 51-77
Ppt 51-77Ppt 51-77
Ppt 51-77
 
Ppt 120-126
Ppt 120-126Ppt 120-126
Ppt 120-126
 
Ppt 138-142
Ppt 138-142Ppt 138-142
Ppt 138-142
 
Ch5 範例
Ch5 範例Ch5 範例
Ch5 範例
 
Ch4 教學
Ch4 教學Ch4 教學
Ch4 教學
 
Ch12 範例
Ch12 範例Ch12 範例
Ch12 範例
 
Ppt 145-149
Ppt 145-149Ppt 145-149
Ppt 145-149
 
Ppt 101-119
Ppt 101-119Ppt 101-119
Ppt 101-119
 

Similar a R code 1

Similar a R code 1 (20)

Ch6
Ch6Ch6
Ch6
 
Ch5
Ch5Ch5
Ch5
 
Ch5 教學
Ch5 教學Ch5 教學
Ch5 教學
 
Ch5
Ch5Ch5
Ch5
 
Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Python入門:5大概念初心者必備
Python入門:5大概念初心者必備
 
Ch9
Ch9Ch9
Ch9
 
ncuma_型別與迴圈.pptx
ncuma_型別與迴圈.pptxncuma_型別與迴圈.pptx
ncuma_型別與迴圈.pptx
 
ncuma_字串.pptx
ncuma_字串.pptxncuma_字串.pptx
ncuma_字串.pptx
 
Ch4
Ch4Ch4
Ch4
 
ncuma_pylab.pptx
ncuma_pylab.pptxncuma_pylab.pptx
ncuma_pylab.pptx
 
Ch8
Ch8Ch8
Ch8
 
Ch8 教學
Ch8 教學Ch8 教學
Ch8 教學
 
ncuma_邏輯與迴圈.pptx
ncuma_邏輯與迴圈.pptxncuma_邏輯與迴圈.pptx
ncuma_邏輯與迴圈.pptx
 
Python 入門
Python 入門 Python 入門
Python 入門
 
Python learn guide
Python learn guidePython learn guide
Python learn guide
 
ncuma_串列.pptx
ncuma_串列.pptxncuma_串列.pptx
ncuma_串列.pptx
 
Ch12
Ch12Ch12
Ch12
 
Ch10
Ch10Ch10
Ch10
 
Ch10 教學
Ch10 教學Ch10 教學
Ch10 教學
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18
 

R code 1

  • 1. ##物件命名 y= 2 x+y x-y x*y x/y x^y 2*x+3*y^2 ls() rm(x) ls() ##物件 #numeric, 實數向量 a <- 49 sqrt(a)#sqrt()開根號 mode(a) b = c(25,36,49) mode(b) length(b) #character string, 文字或字串向量, 通常輸入時, 在文字或字串 兩側加上雙引號. c <- "my classmate ate my homework" c sub("classmate","dog",c)#sub(對象, 要取代的名稱,資料) #logical, 邏輯變數向量 (true or false), 以 TRUE (T) 或 FALSE (F) 呈現,也可以是 1 (T) 與 0 (F) x <- T; y <- F
  • 2. z = c(x, y) z b b>30 larger.than.30 = b>30 rbind(b, larger.than.30)#row(列),橫著排 cbind(b, larger.than.30)#column(欄),縱著排 #dataframe, 資料欄(長度要相同) data.frame(b, larger.than.30) mydata = data.frame(b, larger.than.30);mydata#使用;在 同一行中執行多個完整的指令 mydata[1,]#顯示第一列 mydata[,2]#顯示第二欄 #使用$(錢字號)指定資料欄中的元件) mydata$b mydata$larger.than.30 #使用 attach 綁定資料欄 CO2#R 內建的資料 CO2$uptake CO2$conc uptake attach(CO2) uptake conc test ##讀取資料 #使用 scan()
  • 3. x1 = scan() x2 = scan() scan.failed = scan() getwd()#查詢目前的工作路徑 read.table("random.csv") read.table("random.csv",header = TRUE, sep = ",") random = read.table(file.choose(), header = TRUE, sep = ",") str(random) random random[9,] random$x random$y random$x3#如未填入資料,則 R 讀取資料時會填入 NA ##缺失值 NA, NaN, 與 Null #NA “not available” x <- c(1, 2, 3, NA) x+3 #非數字 “Not a number” log(c(0, 1, 2)) 0/0#infinity Inf - Inf pi / 0 ## = Inf a non-zero number divided by zero creates infinity 0 / 0 ## = NaN 1/0 + 1/0 # Inf 1/0 - 1/0 # NaN #null,物件的長度為 0 z = c()
  • 4. z #任何含有 NA 資料的運算結果都將是 NA 1+NA max(c(NA, 4, 7)) max(c(NA, 4, 7), na.rm=T)#na.rm,移除缺失值 #回到 random.csv 資料來看 max(random$x3) max(random$x3,na.rm=T) mean(random$x3) mean(random$x3,na.rm=T) edit(random)#手動編輯資料,進行一些小修改時很有用 random random.modified = edit(random) fix(random)#直接修改資料 random #封面的兔子 require(onion) data(bunny) p3d(bunny,theta=3,phi=104,box=FALSE)