# R璇█浣跨敤
## 1.Barpolt 鏉″舰鍥攫煋�
barplot(c("鐢风敓"=10, "濂崇敓"=7),main="鐢峰コ鐢熶汉鏁�",col=c("red","orange") )
#浠ヤ笂浠g爜鍙兘浼氱敱浜庢牸寮忕殑闂锛屼腑鏂囧瓧浣撲笉鏄剧ず锛屽洜姝ゅ彲浠ラ�氳繃浣跨敤涓�涓嬪懡浠よ繘琛屼慨鏀�
#par(family='STKaiti')
## 2.鎵撳紑cvs鏂囦欢鍚屾椂鏄剧ず鍓嶅嚑琛�
> tax.tab <- read.csv("/Users/zhangjian/Desktop/R/Rbook-data/taxsamp.csv", header=TRUE, as.is=TRUE)
> print(head(tax.tab))
## 3.鍒涘缓R璇█鏍煎紡鏂囦欢
浣跨敤atom鍒涘缓涓�涓枃浠跺悓鏃朵繚瀛樹负str.r
sum.of.squares <- function(x){
聽 sum(x^2)
}
atom鐨勪笅杞藉湴鍧�[https://atom.io/]
## 4鎵撳紑宸茬粡鍒涘缓鐨剆tr.r
source("/Users/zhangjian/Desktop/str.r")
#璋冪敤宸茬粡鍒涘缓鐨勫嚱鏁�
> sum.of.squares(3)
[1] 9
## 5濡備綍鎵撳嵃鍚戦噺涓殑鐗瑰畾浣嶇疆鐨勬暟鍊�
x <- c(1:3, 10:13)
print(x[4])
[1] 10
## 6鐢熸垚瑙勫垯搴忓垪鐨勫嚱鏁�
### Seq req鍑芥暟
seq(5)
#绛夊悓浜�1:5
seq(2,5)
#绛夊悓浜�2:5
seq(11, 15, by=2)
#浜х敓11,13,15
seq(0, 2*pi, length.out=100)
#浜х敓浠�0鍒扮殑绛夐棿闅斿簭鍒楋紝 搴忓垪闀垮害鎸囧畾涓�100
Pi 灏辨槸鍦嗗懆鐜囩殑鎰忔��
pi =3.1415926535898
### Req鍑芥暟
#rep()鍑芥暟鐢ㄦ潵浜х敓閲嶅鏁板�笺�� 涓轰簡浜х敓涓�涓垵鍊间负闆剁殑闀垮害涓簄鐨勫悜閲忥紝 鐢▁ <- rep(0, n)
#鎶婄涓�涓嚜鍙橀噺閲嶅涓ゆ
> rep(c(1,3), 2)
[1] 1 3 1 3
> rep(c(1,3), c(2,4))
[1] 1 1 3 3 3 3
#鍒欓渶瑕佸埄鐢≧鐨勪竴鑸悜閲忓寲瑙勫垯锛� 鎶婄涓�鑷彉閲忕殑绗竴涓厓绱�1鎸夌収绗簩鑷彉閲忎腑绗竴涓厓绱�2鐨勬鏁伴噸澶嶏紝 鎶婄涓�鑷彉閲忎腑绗簩涓厓绱�3鎸夌収绗簩鑷彉閲忎腑绗簩涓厓绱�4鐨勬鏁伴噸澶嶏紝 缁撴灉鐩稿綋浜巆(1,1,3,3,3,3)銆�
> rep(c(1,3), each=2)
[1] 1 1 3 3
#濡傛灉甯屾湜閲嶅瀹屼竴涓厓绱犲悗鍐嶉噸澶嶅彟涓�鍏冪礌锛岀敤each=閫夐」锛� 姣斿rep(c(1,3), each=2)缁撴灉鐩稿綋浜巆(1,1,3,3)銆�
## 7 identical鍑芥暟
Identical鍑芥暟鐢ㄦ潵鍒ゆ柇涓や釜瀵硅薄鏄惁鏄竴鏍风殑
> identical(c(1,2,3), c(1,2,NA))
[1] FALSE
> identical(c(1L,2L,3L), c(1,2,3))
[1] FALSE
绗簩涓负浠�涔堜細涓嶄竴鏍凤紝鏄洜涓篶(1L,2L,3L) 鏄暣鏁板瀷锛岃�宑(1,2,3)鏄疄鏁板瀷
濡備綍瑙e喅浠ヤ笂闂
鍑芥暟all.equal()鍙互鍦ㄦ瘮杈冩暟鍊煎瀷鏃朵笉鍖哄垎鏁存暟鍨嬩笌瀹炴暟鍨�
> all.equal(c(1,2,3), c(1L,2L,3L))
[1] TRUE
## 8瀛楃涓蹭腑缂哄け鍊肩殑琛ㄧず鏂规硶
> x <- c("huh", "nniun", NA)
> x
[1] "huh"聽 "nniun" NA
**娉ㄦ剰绌哄瓧绗︿覆骞朵笉鑳借嚜鍔ㄨ涓烘槸缂哄け鍊硷紝 瀛楃鍨嬬殑缂哄け鍊间粛鐢∟A琛ㄧず銆�**
## 9 paste()鍑芥暟
瀛楃涓茬殑杩炴帴鏂规硶
> x <- c(11:29)
> y <- c(1:23)
> paste(x,y)
[1] "11 1"聽 "12 2"聽 "13 3"聽 "14 4"聽 "15 5"聽 "16 6"聽 "17 7"聽 "18 8"聽 "19 9"
[10] "20 10" "21 11" "22 12" "23 13" "24 14" "25 15" "26 16" "27 17" "28 18"
[19] "29 19" "11 20" "12 21" "13 22" "14 23"
## 10 as.numeric()鍑芥暟
灏嗗叾浠栫被鍨嬬殑鏁板�艰浆鎹㈡垚鏁板�肩被鍨�
> x <- c("7896778565", "090909")
> x
[1] "7896778565" "090909"聽 聽
> as.numeric(x)
[1] 7896778565聽 聽 聽 90909
璇村疄璇濊兘澶熸甯歌浆鍖栦负鏁板�肩被鍨嬫槸鏁版嵁涔熷氨鏄暟鍊肩殑瀛楃涓茶�屽凡浜�
## 11 as.character()鍑芥暟
鎶婃暟鍊煎瀷杞崲涓哄瓧绗﹀瀷
> as.character((1:5)*5)
[1] "5"聽 "10" "15" "20" "25"
## 12 sprintf()鍑芥暟
涓轰簡鐢ㄦ寚瀹氱殑鏍煎紡鏁板�煎瀷杞崲鎴愬瓧绗﹀瀷锛� 鍙互浣跨敤sprintf()鍑芥暟锛� 鍏剁敤娉曚笌C璇█鐨剆printf()鍑芥暟鐩镐技锛� 鍙笉杩囨槸鍚戦噺鍖栫殑
> sprintf('file%03d.txt', c(1, 99, 100))
[1] "file001.txt" "file099.txt" "file100.txt"
杩欓噷鍙互鐢ㄦ潵鎵归噺鐨勭粰鏂囦欢鍛藉悕 锛屾垨杩樿兘澶熸壒閲忛噸鍛藉悕
## 13 which()鍑芥暟
which()鍑芥暟鍙互杩斿洖婊¤冻鏉′欢鐨勪笅瑙掓爣
> x <- c(3, 4, 3, 5, 7, 5, 9)
> which(x > 5)
[1] 5 7
#馃憜鐨勫懡浠ゆ槸鍦ㄥ悜閲弜涓鎵惧ぇ浜�5鐨勫厓绱狅紝鍚屾椂杩斿洖鍏朵笅瑙掓爣
## 14 濡備綍缁欏悜閲忎腑鐨勫厓绱犲懡鍚�
ages <- c("鏉庢槑"=30, "寮犺仾"=25, "鍒橀"=28)
#鏂规硶涓�
ages <- c(30, 25, 28)
names(ages) <- c("鏉庢槑", "寮犺仾", "鍒橀")
#鏂规硶浜�
ages <- setNames(c(30, 25, 28), c("鏉庢槑", "寮犺仾", "鍒橀"))
鏂规硶涓�
褰撶粰鍚戦噺涓殑鍏冪礌閫氳繃浠ヤ笂鐨勬柟寮忓懡鍚嶅悗锛屽彲浠ラ�氳繃鍛藉悕鍚庣殑鏌ユ壘鍚戦噺涓殑鍏冪礌
鏂规硶涓�
> ages[c("鏉庢槑", "鍒橀")]
聽 鏉庢槑 鍒橀
聽 30聽 28
鏂规硶浜�
> print(ages["鏉庢槑"])
鏉庢槑
聽 30
**浠ヤ笂鎿嶄綔瀹為檯涓婂缓绔嬩簡瀛楃涓插埌鏁板�肩殑鏄犲皠琛�**
#寤虹珛鏄犲皠琛�
> sex.color <- c("鐢�"="blue", "濂�"="red")
> sex.color
聽 聽 鐢仿� 聽 濂�
"blue"聽 "red"
#鏂板缓涓�涓寘鍚俊鎭殑鍚戦噺
> sex <- c("鐢�", "鐢�", "濂�", "濂�", "鐢�", "濂�", "濂�", "濂�", "濂�", "鐢�")
#閫氳繃浣跨敤鏄犲皠琛ㄦ潵鏌ユ壘鍚戦噺瀵瑰簲鐨勫厓绱�
> cols <- sex.color[sex]; print(cols)
聽 聽 鐢仿� 聽 鐢仿� 聽 濂陈� 聽 濂陈� 聽 鐢仿� 聽 濂陈� 聽 濂陈� 聽 濂陈� 聽 濂陈� 聽 鐢�
"blue" "blue"聽 "red"聽 "red" "blue"聽 "red"聽 "red"聽 "red"聽 "red" "blue"
> unname(cols)
[1] "blue" "blue" "red"聽 "red"聽 "blue" "red"聽 "red"聽 "red"聽 "red"聽 "blue"
## 15 unique()鍑芥暟
鍙互灏嗗湪鍚戦噺涓殑閲嶅鍏冪礌缁欏幓閲�
> unique(c(1, 5, 2, 5))
[1] 1 5 2
##16濡備綍鍒ゆ柇涓�涓厓绱犳槸鍚﹀湪涓�涓悜閲忎腑
a %in% x鐢ㄦ潵鍒ゆ柇鍏冪礌鏄惁鏄湪涓�涓泦鍚堜腑
> c(5,6) %in% c(1,5,2)
[1]聽 TRUE FALSE
#R涓殑%in%鏄細浣滀竴涓�昏緫鍒ゆ柇
match(x, table) 鍑芥暟鍜宎 %in% x 鍏锋湁鐩稿悓鐨勪綔鐢�
> match(5, c(1,5,2))
[1] 2
> match(5, c(1,7,2))
[1] NA
#match鍑芥暟浼氬鎵惧嚭鏉ュ尮閰嶇殑鍏冪礌鐨勪笅瑙掓爣锛屽鏋滀笉鍖归厤灏变細鎶A
##17浜わ紝骞讹紝宸泦锛岀浉绛�
intersect(x,y)鍑芥暟鐢ㄦ潵姹備袱涓悜閲忎箣闂寸殑浜ら泦
> intersect(c(5, 7), c(1, 5, 2, 5))
[1] 5
union(x,y)鍑芥暟姹傚苟闆嗭紝缁撴灉涓笉鍚噸澶嶅厓绱�
> union(c(5, 7), c(1, 5, 2, 5))
[1] 5 7 1 2
setdiff(x,y)姹傚樊闆嗭紝鍗硏鐨勫厓绱犱腑涓嶅睘浜巠鐨勫厓绱犵粍鎴愮殑闆嗗悎锛� 缁撴灉涓笉鍚噸澶嶅厓绱�
> setdiff(c(5, 7), c(1, 5, 2, 5))
[1] 7
#杩欎釜鏄痗(5, 7)涓殑鍏冪礌閭d釜鏄湪c(1, 5, 2, 5)涓病鏈夌殑
> setdiff(c(1, 5, 2, 5), c(5, 7))
[1] 1 2
#杩欎釜鏄痗(1, 5, 2, 5)涓殑鍏冪礌閭d釜鏄湪c(5, 7)涓病鏈夌殑
setequal(x,y)鍒ゆ柇涓や釜闆嗗悎鏄惁鐩哥瓑锛� 涓嶅彈娆″簭涓庨噸澶嶅厓绱犵殑褰卞搷
> setequal(c(1,5,2), c(2,5,1))
[1] TRUE
> setequal(c(1,5,2), c(2,5,1,5))
[1] TRUE
#馃憜杩欎釜鏄涓や釜鍚戦噺杩涜鐨勬暣浣撳垽鏂紝涓嶇鏄悜閲忎腑鍏冪礌鐨勯『搴忓拰閲嶅鐨勫奖鍝�
#馃憞杩欎釜鏄涓ょ粍鍚戦噺涓殑鍏冪礌鎸夌収鍥哄畾鐨勯『搴忚繘琛屼竴涓�瀵瑰簲锛岃�屽緱鍑洪�昏緫鍒ゆ柇鎿嶄綔
> c(2,5,1) == c(1,5,2)
[1] FALSE聽 TRUE FALSE
# 闂涓庤В鍐虫柟娉�
## 闂1
鍦≧studio涓殑polt闈㈡澘涓嶆樉绀轰腑鏂�
瑙e喅鏂规硶锛氶渶瑕佹寚瀹氬瓧浣�
par(family='STKaiti') # 鏀瑰瓧浣�, 鍚﹀垯涓嶆樉绀轰腑鏂�
## 闂2
鍦ㄤ娇鐢≧studio鎵撳紑鏂囦欢鐨勬椂鍊欙紝鎶ラ敊锛堝瀛楄妭瀛楃涓叉湁閿欙級
> tax.tab <- read.csv("/Users/zhangjian/Desktop/R/Rbook-data/taxsamp.csv", header=TRUE, as.is=TRUE)
聽 聽 Error in make.names(col.names, unique = TRUE) :
聽 聽 聽 '
杩欓噷鍙兘灏辨槸鍥犱负缂栫爜鐨勯棶棰樺鑷寸殑
Mac绯荤粺涓婁竴鑸殑缂栫爜鏂瑰紡涓簎tf-8 鑰寃in涓枃绯荤粺鐨勪竴鑸紪鐮佹柟寮忎负GBK
鎵�浠ュ湪杩欓噷闇�瑕佸湪Excel淇濆瓨鏂囦欢鐨勬椂鍊欓�夋嫨UTF-8缂栫爜妯″紡灏卞彲浠ヨВ鍐虫闂
## 闂3
婧愭枃浠剁紪鐮侀棶棰�
瑙e喅涓嶅悓骞冲彴涓婇潰鐨勭紪鐮侀棶棰樺鑷寸殑婧愪唬鐮佹棤娉曞鐢ㄧ殑闂
鍦╯ource()鍑芥暟涓彲浠ユ坊鍔爀ncoding=閫夐」
source("/Users/zhangjian/Desktop/str.r", encoding = "UTF-8")
UTF-8涓殑瀛楁瘝闇�瑕佽缃ぇ鍐�
## 闂4
log10(99.99999999999999999锛夋甯告儏鍐典笅鎵�寰楃殑缁撴灉鑲畾涓嶅埌2锛屼负浠�涔堝湪R 涓繘琛屾瘮杈冪殑鏃跺�� 锛屼細鏄剧ず log10(99.99999999999999999锛�= 2
> sele <- (log10(99.99999999999999999) < 2); print(sele)
[1] FALSE
> sele <- (log10(15) < 2); print(sele)
[1] TRUE
> sele <- (log10(99) < 2); print(sele)
[1] TRUE
> log10(99.9999999999999999999)
[1] 2
杩欓噷鍙兘鍜岃绠楁満璁$畻娴偣鏁扮殑鍘熺悊鏈夊叧
> log10(99.999)
[1] 1.999996
> log10(99.9999)
[1] 2
## 闂5
浠ヤ笅鍐呭涓轰粈涔堜細鎶ラ敊鍛�
> x <- 1:100000000
> strsplit(x, ',', fixed=TRUE)[[1]]
Error in strsplit(x, ",", fixed = TRUE) : 闈炲瓧绗﹀弬鏁�
鏄洜涓虹粰鍙橀噺X 鎵�闄勭殑鍊间负涓�涓暟鍊肩被鍨嬩笉鏄瓧绗︿覆绫诲瀷锛屽洜姝ゅ氨鏃犳硶浣跨敤聽 ,'聽 鏉ュ垏鍒嗗瓧绗︿覆
# 灏忔妧宸�
## 1 Atom 鏂囨湰宸ュ叿涓嬭浇鍦板潃
[https://atom.io/]
## 2 濡備綍蹇�熷垏鎹㈠埌涓婁竴姝ュ懡浠�
閿洏鈱笍涓婇潰鐨勪笂涓嬮敭
##3聽 source()璋冪敤婧愮▼搴忔枃浠舵垨鑰呯敤read.csv()璇诲叆鏁版嵁鏂囦欢
闇�瑕佽緭鍏ユ枃浠舵墍鍦ㄧ殑缁濆鐩綍
for example
/Users/zhangjian/Desktop/rstudio-ide.pdf
#濡備綍蹇�熷緱鍒版枃浠剁殑缁濆鐩綍锛屽彲浠ュ皢鏂囦欢鐩存帴鎷栧埌缁堢閲岄潰
## 4 缁濆鐩綍涓庣浉瀵圭洰褰�
缁濆璺緞鏄寚鐩綍涓嬬殑缁濆浣嶇疆锛岀洿鎺ュ埌杈剧洰鏍囦綅缃紝閫氬父鏄粠鐩樼寮�濮嬬殑璺緞
(base) zhangjiandeMacBook-Pro:~ zhangjian$ /Users/zhangjian/Desktop/sly_1_clean_collapsed.fasta
#馃憜鐨勫氨鏄痵ly_1_clean_collapsed.fasta 鏂囦欢鐨勭粷瀵圭洰褰曪紝鍏舵槸浠庣洏绗﹀紑濮嬬殑
鐩稿璺緞灏辨槸鎸囩敱杩欎釜鏂囦欢鎵�鍦ㄧ殑璺緞寮曡捣鐨勮窡鍏跺畠鏂囦欢锛堟垨鏂囦欢澶癸級鐨勮矾寰勫叧绯�
(base) zhangjiandeMacBook-Pro:~ zhangjian$ cd ~/desktop
#鍒囨崲鍒癲ektop鐩綍
(base) zhangjiandeMacBook-Pro:desktop zhangjian$ ls
#鏄剧ずdesktop鐩綍涓嬬殑鎵�鏈夋枃浠讹紝杩欓噷涔熷彲浠ヤ娇鐢╨s -l鏄剧ず鏂囦欢鐨勫叿浣撳弬鏁�
PhaseTank_Tutorial sly_1_clean_collapsed.fasta
R 鏂囩尞搴�
example 鏈爣棰�-1.pdf
mi_rna_app 鏈爣棰�-2
mirdeep2-master 鏈爣棰�-2.pdf
sly-mirna
(base) zhangjiandeMacBook-Pro:desktop zhangjian$ head ~/desktop/sly_1_clean_collapsed.fasta
>sl1_0_x62057
AGTTACTAATTCATGATCTGGC
>sl1_62057_x45550
TCGGACCAGGCTTCATTCCCC
>sl1_107607_x43623
CTAACAGACCGGTAGACTTGAAC
>sl1_151230_x23996
ACAAACGACTCTCGGCAA
>sl1_175226_x16618
CCCCAAGATGAGTGCTCTCCT
#聽 鐭ヨ瘑鐐�
---- 灏忕煡璇嗙偣-1
NA 浠h〃鐨勬槸"缂哄け"锛孨ULL 浠h〃鐨勬槸"涓嶅瓨鍦�"銆�
NA 缂哄け灏辨兂鍗犱綅绗︼紝浠h〃杩欓噷娌℃湁涓�涓�硷紝浣嗕綅缃瓨鍦ㄣ��
NULL 浠h〃鐨勫氨鏄暟鎹笉瀛樺湪銆�
---- 灏忕煡璇嗙偣-2
UTF-8锛�8-bit Unicode Transformation Format锛夛紙Mac绯荤粺鍐呯疆锛�
姹夊瓧鍐呯爜鎵╁睍瑙勮寖锛岀ОGBK锛屽叏鍚嶄负銆婃眽瀛楀唴鐮佹墿灞曡鑼�(GBK)銆�1.0鐗� 锛坵in绯荤粺鍐呯疆锛�
---- 灏忕煡璇嗙偣-3
鍙橀噺鐢ㄦ潵淇濆瓨杈撳叆鐨勫�兼垨璁$畻鐨勭粨鏋溿�� 鍙橀噺鍙互瀛樻斁鍚勭涓嶅悓绫诲瀷鐨勫�硷紝 濡傚崟涓暟鍊笺�佸涓暟鍊硷紙绉颁负鍚戦噺锛夈�佸崟涓瓧绗︿覆銆佸涓瓧绗︿覆锛堢О涓哄瓧绗﹀瀷鍚戦噺锛夛紝绛夌瓑銆� 鍗曚釜鏁板�肩О涓烘爣閲�
---- 灏忕煡璇嗙偣-4
缁勫悎鏁扮殑姒傚康
---- 灏忕煡璇嗙偣-5
娴偣鏁�
鍗曠簿搴︽诞鐐规暟
鍗曠簿搴︽诞鐐规暟鏄敤鏉ヨ〃绀哄甫鏈夊皬鏁伴儴鍒嗙殑瀹炴暟锛屼竴鑸敤浜庣瀛﹁绠椼�傚崰鐢�4涓瓧鑺傦紙32浣嶏級瀛樺偍绌洪棿锛屽寘鎷鍙蜂綅1浣嶏紝闃剁爜8浣嶏紝灏炬暟23浣嶃�傚叾鏁板�艰寖鍥翠负-3.4E38锝�3.4E38锛屽崟绮惧害娴偣鏁版渶澶氭湁7浣嶅崄杩涘埗鏈夋晥鏁板瓧锛屽崟绮惧害娴偣鏁扮殑鎸囨暟鐢ㄢ�淓鈥濇垨鈥渆鈥濊〃绀恒��
濡傛灉鏌愪釜鏁扮殑鏈夋晥鏁板瓧浣嶆暟瓒呰繃7浣嶏紝褰撴妸瀹冨畾涔変负鍗曠簿搴﹀彉閲忔椂锛岃秴鍑虹殑閮ㄥ垎浼氳嚜鍔ㄥ洓鑸嶄簲鍏ャ��
鍙岀簿搴︽诞鐐规暟
鍙岀簿搴︽诞鐐规暟(double)鏄绠楁満浣跨敤鐨勪竴绉嶆暟鎹被鍨嬶紝浣跨敤 64 浣嶏紙8瀛楄妭锛� 鏉ュ瓨鍌ㄤ竴涓诞鐐规暟銆� 瀹冨彲浠ヨ〃绀哄崄杩涘埗鐨�15鎴�16浣嶆湁鏁堟暟瀛楋紝鍏跺彲浠ヨ〃绀虹殑鏁板瓧鐨勭粷瀵瑰�艰寖鍥村ぇ绾︽槸锛�-1.79E+308 ~ +1.79E+308~
---- 灏忕煡璇嗙偣-6
鍚戦噺涓庢爣閲弜1 + x2
---- 灏忕煡璇嗙偣-7
鐭╅樀缃崲
> M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
> print(M)
聽 聽 [,1] [,2] [,3]
[1,]聽 聽 2聽 聽 6聽 聽 5
[2,]聽 聽 1聽 10聽 聽 4
#灏嗙煩闃礛杩涜缃崲
> t(M)
聽 聽 [,1] [,2]
[1,]聽 聽 2聽 聽 1
[2,]聽 聽 6聽 10
[3,]聽 聽 5聽 聽 4
#灏嗙疆鎹㈠悗鐨勭煩闃佃祴鍊间簬v
> v <- t(M)
> print(v)
聽 聽 [,1] [,2]
[1,]聽 聽 2聽 聽 1
[2,]聽 聽 6聽 10
[3,]聽 聽 5聽 聽 4
#鐭╅樀鐩镐箻
> y = M %*% v
> print(y)
聽 聽 [,1] [,2]
[1,]聽 65聽 82
[2,]聽 82聽 117
**鐭╅樀鐩镐箻鐨勫師鐞�**
鐭╅樀鐩镐箻鏈�閲嶈鐨勬柟娉曟槸涓�鑸煩闃典箻绉�傚畠鍙湁鍦ㄧ涓�涓煩闃电殑鍒楁暟锛坈olumn锛夊拰绗簩涓煩闃电殑琛屾暟锛坮ow锛夌浉鍚屾椂鎵嶆湁鎰忎箟聽 銆備竴鑸崟鎸囩煩闃典箻绉椂锛屾寚鐨勪究鏄竴鑸煩闃典箻绉�備竴涓猰脳n鐨勭煩闃靛氨鏄痬脳n涓暟鎺掓垚m琛宯鍒楃殑涓�涓暟闃点�傜敱浜庡畠鎶婅澶氭暟鎹揣鍑戝湴闆嗕腑鍒颁簡涓�璧凤紝鎵�浠ユ湁鏃跺�欏彲浠ョ畝渚垮湴琛ㄧず涓�浜涘鏉傜殑妯″瀷锛屽鐢靛姏绯荤粺缃戠粶妯″瀷銆�
(img)
(img)
(img)
(img)
---- 灏忕煡璇嗙偣-8
e鏁板甯告暟
e锛屼綔涓烘暟瀛﹀父鏁帮紝鏄嚜鐒跺鏁板嚱鏁扮殑搴曟暟銆傛湁鏃惰绉颁负娆ф媺鏁帮紙Euler's number锛夛紝浠ョ憺澹暟瀛﹀娆ф媺鍛藉悕锛涜繕鏈変釜杈冨皯瑙佺殑鍚嶅瓧绾崇毊灏斿父鏁帮紝鐢ㄦ潵绾康鑻忔牸鍏版暟瀛﹀绾︾堪路绾崇毊灏斿紩杩涘鏁般�傚畠鏄竴涓棤闄愪笉寰幆灏忔暟锛屾暟鍊肩害鏄紙灏忔暟鐐瑰悗20浣嶏紝OEIS鈥夾001113锛夛細e = 2.71828182845904523536
> exp(1)
[1] 2.718282
---- 灏忕煡璇嗙偣-9
e鏁板甯告暟
e锛屼綔涓烘暟瀛﹀父鏁帮紝鏄嚜鐒跺鏁板嚱鏁扮殑搴曟暟銆傛湁鏃惰绉颁负娆ф媺鏁帮紙Euler's number锛夛紝浠ョ憺澹暟瀛﹀娆ф媺鍛藉悕锛涜繕鏈変釜杈冨皯瑙佺殑鍚嶅瓧绾崇毊灏斿父鏁帮紝鐢ㄦ潵绾康鑻忔牸鍏版暟瀛﹀绾︾堪路绾崇毊灏斿紩杩涘鏁般�傚畠鏄竴涓棤闄愪笉寰幆灏忔暟锛屾暟鍊肩害鏄紙灏忔暟鐐瑰悗20浣嶏紝OEIS鈥夾001113锛夛細e = 2.71828182845904523536
> exp(1)
[1] 2.718282
---- 灏忕煡璇嗙偣-10
甯冨皵鏁版嵁绫诲瀷
甯冨皵锛堣嫳璇細Boolean锛夋槸璁$畻鏈虹瀛︿腑鐨勯�昏緫鏁版嵁绫诲瀷锛屼互鍙戞槑甯冨皵浠f暟鐨勬暟瀛﹀涔旀不路甯冨皵涓哄悕銆傚畠鏄彧鏈変袱绉嶅�肩殑鍘熷绫诲瀷锛岄�氬父鏄湡鍜屽亣銆傚竷灏旀暟鎹被鍨嬩富瑕佷笌鏉′欢璇彞鐩稿叧绯伙紝鏉′欢璇彞閫氳繃鏍规嵁寮�鍙戜汉鍛樻寚瀹氱殑鏉′欢寮忥紝鏇存敼绋嬪簭鎺у埗娴佹潵鍏佽璇勪及璇彞鐨勮繍绠楀�间负鐪熸垨鍋�(鍗虫潯浠舵垚绔嬫垨涓嶆垚绔�)銆傝繖鏄竴绉嶆洿骞挎硾鐨勯�昏緫鏁版嵁绫诲瀷鐨勭壒娈婃儏鍐碉紙鍙傝姒傜巼閫昏緫锛夛紞閫昏緫骞朵笉鎬绘槸鍙睘浜庡竷灏旂被鍨嬬殑銆�
鍦ㄤ竴浜涜瑷�涓紝甯冨皵鏁版嵁绫诲瀷琚畾涔変负鍙唬琛ㄥ浜庝袱涓湡鍊笺�備緥濡傦紝ISO SQL:1999鏍囧噯瀹氫箟浜嗕竴涓猄QL甯冨皵鍨嬪彲浠ュ偍瀛樹笁涓彲鑳界殑鍊硷細鐪熴�佸亣銆佹湭鐭ワ紙SQL null琚綋浣滄湭鐭ョ湡鍊兼潵澶勭悊锛屼絾浠呬粎鍦ㄥ竷灏斿瀷涓娇鐢級銆傚湪姝ゆ儏鍐典笅锛屾湭鐭ュ厛浜庣湡鍙婂亣锛屽洜涓哄竷灏斿瀷涓�寮�濮嬫槸鏈湁瀹為檯鍊硷紝鍏跺�兼槸unknown(涔熸湁鏈轰細鏄殢鏈哄��)鑰岄潪鐪熴��
---- 灏忕煡璇嗙偣-11
缂哄け鍊硷紝缁撴灉鍙兘涓虹己澶卞��
> c(1, NA, 3) > 2
[1] FALSE聽 聽 NA聽 TRUE
---- 灏忕煡璇嗙偣-12
[:space:] 鍖归厤绌虹櫧瀛楃锛屽寘鎷┖鏍硷紝tab
gsub('[[:space:]]+', ' ', 'a聽 cat聽 in a box', perl=TRUE)
#[[:space:]]+ 杩欎釜姝e垯琛ㄨ揪寮忔槸琛ㄧず鏄槸澶氫釜绌烘牸鎴栬�卼ab閿殑鎰忔��
#鍚屾椂浣跨敤涓�涓┖鏍兼浛鎹㈠涓┖鏍�
---- 灏忕煡璇嗙偣-13
R璇█閲岄潰鐨勫ぇ鎷彿銆佷腑鎷彿銆佸皬鎷彿鐨勪綔鐢�
()瀹氫箟鍑芥暟鐨勫弬鏁�
> sprintf('file%03d.txt', c(1, 99, 100))
[1] "file001.txt" "file099.txt" "file100.txt"
#鍦�()閲岄潰鐨�'file%03d.txt', c(1, 99, 100)鏄嚱鏁皊printf鎵�瀹氫箟鐨勫弬鏁�
()鍦ㄨ繍绠椾腑浣滀负浼樺厛绾�
> 3*(4+5-2)-34
[1] -13
> 3*4+5-2-34
[1] -19
#浠ヤ笂浠g爜琛ㄦ槑浜嗗洜涓烘湁()鐨勫瓨鍦紝杩愮畻鐨勭粨鏋滃彂鐢熶簡鍙樺寲
[] 鐢ㄥ仛瀵硅薄鐨勫厓绱犵储寮�
> x = seq(99,699,by=3)
> x[5]
[1] 111
> print(x)
聽 [1]聽 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153
[20] 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210
[39] 213 216 219 222 225 228 231 234 237 240 243 246 249 252 255 258 261 264 267
[58] 270 273 276 279 282 285 288 291 294 297 300 303 306 309 312 315 318 321 324
[77] 327 330 333 336 339 342 345 348 351 354 357 360 363 366 369 372 375 378 381
[96] 384 387 390 393 396 399 402 405 408 411 414 417 420 423 426 429 432 435 438
[115] 441 444 447 450 453 456 459 462 465 468 471 474 477 480 483 486 489 492 495
[134] 498 501 504 507 510 513 516 519 522 525 528 531 534 537 540 543 546 549 552
[153] 555 558 561 564 567 570 573 576 579 582 585 588 591 594 597 600 603 606 609
[172] 612 615 618 621 624 627 630 633 636 639 642 645 648 651 654 657 660 663 666
[191] 669 672 675 678 681 684 687 690 693 696 699
#浣跨敤seq鍑芥暟鐢熸垚浜嗕竴涓粠99鍒�699锛屾闀夸负3鐨勫悜閲忥紝鐒跺悗鍙互浣跨敤x[5]锛屽杩欎釜鍚戦噺涓殑鏁板�艰繘琛岀储寮�
{} 浣滀负浠g爜娈佃惤鐨勮捣姝�