Top Banner
R 教教教教 1 Using R for basic statistical analyses 1. 如如如如 R 如 Rstudio 如 (如如如如如如如如) A. 如如 R 如 體:https://cloud.r-project.org/
7

chao.stat.nthu.edu.twchao.stat.nthu.edu.tw/wordpress/wp-content/uploads... · Web viewUsing R for basic statistical analyses. 如何安裝. R. 與. Rstu. dio. 軟體 (依照下列.

Mar 02, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: chao.stat.nthu.edu.twchao.stat.nthu.edu.tw/wordpress/wp-content/uploads... · Web viewUsing R for basic statistical analyses. 如何安裝. R. 與. Rstu. dio. 軟體 (依照下列.

R 教學講義 1

Using R for basic statistical analyses

1. 如何安裝 R 與 Rstudio 軟體 (依照下列步驟安裝)

A. 下載 R 軟體的安裝檔案:https://cloud.r-project.org/

Page 2: chao.stat.nthu.edu.twchao.stat.nthu.edu.tw/wordpress/wp-content/uploads... · Web viewUsing R for basic statistical analyses. 如何安裝. R. 與. Rstu. dio. 軟體 (依照下列.

R 教學講義 2

B. 下載 RStudio 軟體的安裝檔案https://www.rstudio.com/products/rstudio/download/

Page 3: chao.stat.nthu.edu.twchao.stat.nthu.edu.tw/wordpress/wp-content/uploads... · Web viewUsing R for basic statistical analyses. 如何安裝. R. 與. Rstu. dio. 軟體 (依照下列.

R 教學講義 3

C. 安裝 R 軟體

D. 安裝 RStudio 軟體

E. 開啟 RStudio

Page 4: chao.stat.nthu.edu.twchao.stat.nthu.edu.tw/wordpress/wp-content/uploads... · Web viewUsing R for basic statistical analyses. 如何安裝. R. 與. Rstu. dio. 軟體 (依照下列.

a <- c(0,1,2,3,4,5,6,7,8) a <- 0:8alength(a) b <- matrix(a,3,3)bc <- matrix(a,3,3,byrow = TRUE) cd <- rep(1:4,1:4)de <- seq(0,1,0.1)e

range(b)head(b,2)

a[2] #取出 a向量中的第 2個數值c[1,2] #取出 c矩陣中的第 1列第 2行數值c[1,] #取出 c矩陣中的第 1列向量

R 教學講義 4

2. R 基本函數介紹A. 向量函數:為輸入資料與檢視資料的函數

常用的函數:c() 建立向量或聯結不同向量 c: concatenated or combinedrep(x,t) x 為要複製的向量或因子,t 為複製次數seq(x,y,t) 從 x 開始到 y 結束,公差為 t 的等差數列length(x) 算出 x 元素個數matrix(x,r,c) x 為資料,r為列數,c為行數range(x) 計算元素 x 的範圍,()內可為向量或矩陣head(x,n) 查看資料 x 前 n 列(筆)資料,x 可為向量或矩陣sort(x) 對 x 依大小排序

Example: 產生一個向量並計算其長度、產生一個數字由 0~8 的 3*3 矩陣

求出 b 矩陣的範圍,以及前兩列資料

取出向量或矩陣內元素

Page 5: chao.stat.nthu.edu.twchao.stat.nthu.edu.tw/wordpress/wp-content/uploads... · Web viewUsing R for basic statistical analyses. 如何安裝. R. 與. Rstu. dio. 軟體 (依照下列.

sum(a); prod(a)min(a); max(a)x = 2; x^a ; a^x log(a, base = x)sin(x);cos(x);tan(x)c %*% c

#解出 x^2 – 1=0 之根polyroot(c(-1,0,1))#解出 x^2 + 1=0 之根polyroot(c(1,0,1))#解出 x^3 +2x+ 1=0 之根polyroot(c(1, 2, 0,1))

R 教學講義 5

B. 計算函數: 常用的函數:

sum(x), prod(x) 向量 x 所有元素的總和、所有元素相乘min(x), max(x) 向量 x 所有元素的最小値、最大値sqrt(x) 對向量 x 的每個元素開根號abs(x) 對向量 x 的每個元素取絕對值x^a ; a^x 向量 x 的 a 次方;對向量 x 取以 a 為底的數log(x, base = a) 對向量 x 的每個元素取以 a 為底的對數sin(x);cos(x);tan(x);cot(x)

對向量 x 的每個元素作三角函數x %*% y 矩陣乘法,矩陣 x 乘矩陣 y

Example:

C. 多項式方程式解根函數: 常用的函數:

polyroot(x) x 為要解之方程式的每項係數 Example:

D. 繪圖函數: 常用的函數:

plot(x, y, type,…) 一般繪圖函數。x 為 x 軸數值,y 為 y 軸數值。type 為圖形類型,"l” for lines, "p” for points, "c" for dashed line, ….

curve(expr, from, to,…) 畫曲線函數,expr 為要繪圖的函數,from,to 為函數的範圍

lines(x, y, type,…) 在當前繪圖中加入另一函數曲線

Page 6: chao.stat.nthu.edu.twchao.stat.nthu.edu.tw/wordpress/wp-content/uploads... · Web viewUsing R for basic statistical analyses. 如何安裝. R. 與. Rstu. dio. 軟體 (依照下列.

#對函數 x^3-8*x^2+50*x+5,當 x=-10~10畫曲線圖#Method1x <- -10:10y <- x^3-8*x^2+50*x+5plot(x, y, xlab="x", ylab="y", type="l")#Method2curve(x^3-8*x^2+50*x+5, from=-10, to=10 , xlab="x", ylab="y")lines(x,-x^4-200, xlab="x", ylab="y",type="l",col="red")abline(a = 100,b = 50, col="blue")abline(h = 0, lty = 2)abline(v = 0, lty = 2)

Data <- read.csv("C:/Users/STAT_NB_L1/Desktop/2021Spring統計資料分析/2.25/Survey1.csv", header = TRUE)Data

R 教學講義 6

abline(a, b, h, v,…) 給當前繪圖添加一條或多條直線,a為繪製直線的截距,b為斜率,h為繪製水平線的縱軸值,v為繪製鉛錘線的橫軸值

Example:

E. 資料讀取函數: 常用的函數:

read.csv(file, header) file 為完整路徑+檔名,header 若為 True,數據第一列為名稱

Example:

Week 1-2 HW:

安裝 R 和 RStudio, 並執行上述例題 , 將結果於 (3/11)13:00 前上傳網站

助教時間及 E-mail (839 教室 ) 司徒子謙 [email protected] 禮拜二下午 2:20-3:10 沈巧盈 [email protected] 禮拜三早上 11:10-12:00 吳昭宛 [email protected] 禮拜二早上 10:10-11:00

Page 7: chao.stat.nthu.edu.twchao.stat.nthu.edu.tw/wordpress/wp-content/uploads... · Web viewUsing R for basic statistical analyses. 如何安裝. R. 與. Rstu. dio. 軟體 (依照下列.

R 教學講義 7

王軒宇 [email protected] 禮拜一下午 2:20-3:10 林建佑 [email protected] 禮拜四早上 10:10-11:00