- 安裝R 與Rstudio
- Rstudio 功能簡介
- 上手R 程式撰寫
Wush Wu
國立台灣大學
sudo sh -c 'echo "deb http://cran.csie.ntu.edu.tw/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list'
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9
sudo apt-get update && apt-get install -y r-base libcurl4-openssl-dev libxml2-dev espeak








expression(敘述)expression
expression>代表「R 正準備接受下一個expression」Enter代表一個expression的結束expression
expression+代表「R 認為expression沒有結束」ESC可以中斷expressionexpression#之後的程式碼不會被處理,稱為「註解」1 + 1
## [1] 2
#1 + 1
0或1是一個位元(bit)0或1是一個位元組(Bytes)0 : 女性、1:男性# Windows
system("wmic memorychip get capacity")
# OS X
system("top -l 1 | grep PhysMem: | awk '{print $10}'")
# Linux
system("free -h")
或是看課堂老師的說明
y = x + 1
x 代入 1 則 y 為 2
x 代入 2 則 y 為 3
"所夾住的文字,在程式碼中會視為字串(character)1 + 1
## [1] 2
"1 + 1"
## [1] "1 + 1"
a <- 1
plot(cars, main = "a")

plot(cars, main = a)

2300 * 10000 * (10 + 4) / 1024 / 1024
## [1] 307.0831
x萬人的身份證字號與性別需要多大的記憶體(MB)?x * 10000 * (10 + 4) / 1024 / 1024
x = 270
x * 10000 * (10 + 4) / 1024 / 1024
## [1] 36.04889
x = 400
x * 10000 * (10 + 4) / 1024 / 1024
## [1] 53.40576
描述從名字到值的關係
r
x = 270
x是名字、值則是270
x = 270這類型的程式碼被稱為assignment(賦值)
assignment是不是一種expression?在R 裡面,以下都是assignment
x = 1
x <- 1
# 雲林縣的人口有69萬人、苗栗縣有56萬人、新竹縣有55萬人、嘉義縣有52萬人,
# 請問儲存以上縣市所有的身份證字號(用10個文字,10 Bytes)、性別(用整數,4 Bytes)與電話號碼(用整數4 Bytes)
# 需要多少記憶體(MB)? 請用x 的方式寫
x <- <數字>
x * 10000 * (<每儲存一個人的資訊所需要的記憶體大小>) / 1024 / 1024
<數字>與<每儲存一個人的資訊所需要的記憶體大小>替換成你的expressionx <- <數字>可以算出不同的答案x <- 69 # 雲林縣的人口有69萬人
x * 10000 * (10 + 4 + 4) / 1024 / 1024
## [1] 11.84464
x <- 56 # 苗栗縣有56萬人
x * 10000 * (10 + 4 + 4) / 1024 / 1024
## [1] 9.613037
x <- 55 # 新竹縣有55萬人
x * 10000 * (10 + 4 + 4) / 1024 / 1024
## [1] 9.441376
x, 輸出是x * 10000 * (10 + 4 + 4) / 1024 / 1024
f <- function(x) x * 10000 * (10 + 4 + 4) / 1024 / 1024
f <- function(x) x * 10000 * (10 + 4 + 4) / 1024 / 1024
f(69) # 雲林縣的人口有69萬人
## [1] 11.84464
f(56) # 苗栗縣有56萬人
## [1] 9.613037
f(55) # 新竹縣有55萬人
## [1] 9.441376
f(52) # 嘉義縣有52萬人
## [1] 8.926392
# 1. 利用地圖資料與natural earth data 上的 urban 資料,畫出類似地球夜景的效果
# 2. 利用大圓畫出航道
library(maps)
library(maptools)
library(geosphere)
library(plyr)
library(ggplot2)
library(sp)
library(rgeos)
library(grid)
library(gridExtra)
airports <- read.csv(url("http://www.stanford.edu/~cengel/cgi-bin/anthrospace/wp-content/uploads/2012/03/airports.csv"), as.is=TRUE, header=TRUE)
flights <- read.csv(url("http://www.stanford.edu/~cengel/cgi-bin/anthrospace/wp-content/uploads/2012/03/PEK-openflights-export-2012-03-19.csv"), as.is=TRUE, header=TRUE)
# ... 待續
a <- c(1, 2, 3, 4)
# 平均數
mean(a)
## [1] 2.5
f <- mean
f(a)
## [1] 2.5

mean: 函數的名字(、): 括號。名字後面接括號,代表這個名字的變數是一種函數a: arguments(參數)mean(x)mean: 算平均數sd: 算樣本標準差mean(x): 計算x 的平均數mean(y): 計算y 的平均數mean(x, na.rm = TRUE): 計算x 的平均數,不包含遺失值XML、xml2、jsonlitehttrsupcdplyrggplot2Lahmanexamplesdemovignettesinstall.packages("ctv")
library(ctv)
library(ctv)
# ctv::available.views()
available.views()
##
## CRAN Task View
## --------------
## Name: DifferentialEquations
## Topic: Differential Equations
## Maintainer: Karline Soetaert and Thomas Petzoldt
## Contact: karline.soetaert@nioz.nl
## Version: 2017-01-31
## Repository: http://cran.csie.ntu.edu.tw
## Packages: adaptivetau, bvpSolve*, CollocInfer, deSolve*, deTestSet,
## ecolMod, FME, GillespieSSA, mkin, nlmeODE, odeintr,
## PBSddesolve, PBSmodelling, pomp, pracma, primer, PSM,
## ReacTran, rootSolve*, scaRabee, sde*, Sim.DiffProc, simecol
## (* = core package)
args(install.packages)
## function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
## type), method, available = NULL, destdir = NULL, dependencies = NA,
## type = getOption("pkgType"), configure.args = getOption("configure.args"),
## configure.vars = getOption("configure.vars"), clean = FALSE,
## Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
## libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
## ...)
## NULL
install.packages 負責安裝套件
電腦需要知道動作的細節才能運作:
## function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
## type), method, available = NULL, destdir = NULL, dependencies = NA,
## type = getOption("pkgType"), configure.args = getOption("configure.args"),
## configure.vars = getOption("configure.vars"), clean = FALSE,
## Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
## libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
## ...)
## NULL
install.packages的參數有: pkg、lib、repos...
pkg 代表「要安裝的套件的名稱」repos 代表「提供套件的網站」## function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
## type), method, available = NULL, destdir = NULL, dependencies = NA,
## type = getOption("pkgType"), configure.args = getOption("configure.args"),
## configure.vars = getOption("configure.vars"), clean = FALSE,
## Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
## libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
## ...)
## NULL
install.packages("ctv")
pkg : "ctv"## function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
## type), method, available = NULL, destdir = NULL, dependencies = NA,
## type = getOption("pkgType"), configure.args = getOption("configure.args"),
## configure.vars = getOption("configure.vars"), clean = FALSE,
## Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
## libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
## ...)
## NULL
install.packages("ctv", "http://cran.csie.ntu.edu.tw")
pkg : "ctv"lib : "http://cran.csie.ntu.edu.tw"## function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
## type), method, available = NULL, destdir = NULL, dependencies = NA,
## type = getOption("pkgType"), configure.args = getOption("configure.args"),
## configure.vars = getOption("configure.vars"), clean = FALSE,
## Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
## libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
## ...)
## NULL
install.packages("ctv", repos = "http://cran.csie.ntu.edu.tw")
pkg : "ctv"repos : "http://cran.csie.ntu.edu.tw"## function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
## type), method, available = NULL, destdir = NULL, dependencies = NA,
## type = getOption("pkgType"), configure.args = getOption("configure.args"),
## configure.vars = getOption("configure.vars"), clean = FALSE,
## Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
## libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
## ...)
## NULL
install.packages("ctv", repo = "http://cran.csie.ntu.edu.tw")
pkg : "ctv"repos : "http://cran.csie.ntu.edu.tw"## function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
## type), method, available = NULL, destdir = NULL, dependencies = NA,
## type = getOption("pkgType"), configure.args = getOption("configure.args"),
## configure.vars = getOption("configure.vars"), clean = FALSE,
## Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
## libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
## ...)
## NULL
install.packages("ctv", "http://cran.csie.ntu.edu.tw", lib = "/home/wush/Project/2017-DS-Programming/.lib")
pkg : "ctv"lib : "/home/wush/Project/2017-DS-Programming/.lib"repos : "http://cran.csie.ntu.edu.tw"## function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
## type), method, available = NULL, destdir = NULL, dependencies = NA,
## type = getOption("pkgType"), configure.args = getOption("configure.args"),
## configure.vars = getOption("configure.vars"), clean = FALSE,
## Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
## libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
## ...)
## NULL
install.packages("ctv")
repos : getOption("repos")destdir : NULLargs(strsplit)
## function (x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
## NULL
strsplit("abcdef", TRUE, split = "cd")
## [[1]]
## [1] "ab" "ef"
x、split、fixed、perl與useBytes的值各為何?