C-x C-e
, 爽到根本停不下來!C-x C-e
執行R的S表達式C-x C-e
執行R的S表達式el-get-install ESS
C-c C-k
打開R的Repl, C-c C-l
eval當前文件緩衝到Repl裏面C-x C-e
fun r lisp!;; 將這裏的配置放到啓動腳本init.el或者是`.emacs` (defun ess-eval-sexp (vis) (interactive "P") (save-excursion (backward-sexp) (let ((end (point))) (forward-sexp) (ess-eval-region (point) end vis "Eval sexp")))) (add-hook 'ess-mode-hook (lambda () (define-key global-map (kbd "C-x C-e") 'ess-eval-sexp) ))
# define (function (y) (function (x) ('+' (x, y)))) # call ((function (x) x) (1)) #=> [1] 1
## 用高階函數 和 %>% 管道來 代替let, function(a=111,b=222,c=function(...){...} ) { ... } ## function的默認參數就是一個局部變量: function(a=1, b=2) <=> let[a 1 b 2] ((function (x, y=(function (i) ('*' (i, 2))) ) (y (x))) (2)) #=> [1] 4 ## 用強大的函數管道 (library (magrittr)) ((c (1, 2, 3)) %>% (function (x) (Map ((function (x) ('+' (x, 100))), x))) %>% (function (x) (Reduce ('+', x)) ) ) #=> [1] 306 ## let複用前面的變量定義 ((function (x, y=('*' (x, 2))) y) (100)) #=> [1] 200 ## 綜合例子: function裏面的默認參數,當let來用,能夠用前面定義的變量(x,y=x),可是不能覆蓋前面定義的變量(x,x=1) ((function (y, x, mx=(as.matrix (x)), cx=(cbind (Intercept=1, mx))) ('%*%' (('%*%' ((solve ('%*%' ((t (cx)), cx))), (t (cx)))), y)) ) -> reg) (reg (y=(launch$distress_ct), x=(launch [3]))) ## [,1] ## Intercept 4.30158730 ## temperature -0.05746032
('if' (0, ('==' (1, 1)), ('==' (2, 1)))) #=> [1] FALSE
('plot' (('rnorm' (10)), ('rnorm' (10)))) # 加了額外的參數 ('plot' (('rnorm' (10)), ('rnorm' (10)), type='b'))
(Reduce ('*', 1:10))
((function (x) ('if' (('%%' (x, 2)), x, 0))) (2)) #=> [1] 0 # call (Filter ((function (x) ('if' (('%%' (x, 2)), x, 0))), 1:10)) #=> [1] 1 3 5 7 9
(Map ((function (x) ('+' (x, 100))), 1:3)) # => [[1]] [1] 101 [[2]] [1] 102 [[3]] [1] 103
## 1d: 1維 # 若是原本是前綴的表達方式的函數,引號'c'能夠省略,function除外必須加引號 (c (1, 1, 3)) #=> [1] 1 1 3 ((c (1, 8, 3)) [2]) #=> [1] 8 ((c ("A", "B", "C")) -> defvar) #=> [1] "A" "B" "C"
# levels是不能重複出現的 (factor ((c ("1", "1", "3", "11", "9", "8")), levels=(c ("A", "B", "C", "AA", "BB", "CC")))) #=> [1] <NA> <NA> <NA> <NA> <NA> <NA> Levels: A B C AA BB CC ### 替換一下數據名稱,把B替換爲"良性腫塊" ==>> 因子的意義: 賦予跟多的標籤的意義 ((factor (wbcd$diagnosis, levels=(c ("B", "M")), labels=(c ("良性腫塊", "惡性腫塊")))) -> wbcd$diagnosis) #=> diagnosis radius_mean texture_mean perimeter_mean area_mean smoothness_mean 1 惡性腫塊 17.990 10.38 122.80 1001.0 0.11840 2 惡性腫塊 20.570 17.77 132.90 1326.0 0.08474 21 良性腫塊 13.080 15.71 85.63 520.0 0.10750
## 1d: 1維 (list (11, "aa", FALSE)) #=> [[1]] [1] 11 [[2]] [1] "aa" [[3]] [1] FALSE
## nd: N維 (1:12) ##=> [1] 1 2 3 4 5 6 7 8 9 10 11 12 ##class: [1] "integer" (array (1:12)) #=>class [1] "array" ##=> [1] 1 2 3 4 5 6 7 8 9 10 11 12 (array (1:12, (c (2, 3, 2)))) #=>class [1] "array" ## [,1] [,2] [,3] ## [1,] 7 9 11 ## [2,] 8 10 12 ##
## 2d: 2維 ((data.frame ( ID=(c (11,12,13)), Name=(c ("Devin","Edward","Wenli")), Gender=(c ("M","M","F")), Birthdate=(c ("1984-12-29","1983-5-6","1986-8-8")))) -> pt_data) #=> ID Name Gender Birthdate 1 11 Devin M 1984-12-29 2 12 Edward M 1983-5-6 3 13 Wenli F 1986-8-8 ## get: (pt_data [1, 2]) #=> 第一行,第二列 [1] Devin Levels: Devin Edward Wenli (pt_data [,3]) #=> 只是第三列 [1] M M F Levels: F M ((pt_data [-1]) [-2]) #=> 去除第一,而後再去除第二列 Name Birthdate 1 Devin 1984-12-29 2 Edward 1983-5-6 3 Wenli 1986-8-8 (pt_data$Birthdate) #=> 取某一列 [1] 1984-12-29 1983-5-6 1986-8-8 Levels: 1983-5-6 1984-12-29 1986-8-8 (pt_data [2:3]) # 取範圍 Name Gender 1 Devin M 2 Edward M 3 Wenli F
## 2d: 2維 (matrix ((c (1, 2, 1, 3, 5, 8)), nrow=2)) #=> 2行->3列 [,1] [,2] [,3] [1,] 1 1 5 [2,] 2 3 8 (matrix ((c (1, 2, 1, 3, 5, 8)), ncol=2)) #=> [,1] [,2] [1,] 1 3 [2,] 2 5 [3,] 1 8 (matrix ((c (1, 2, 4, 3)), ncol=1)) #=> 單列矩陣 [,1] [1,] 1 [2,] 2 [3,] 4 [4,] 3 (matrix ((c (1, 2, 4, 3)), nrow=1)) #=> 單行矩陣 [,1] [,2] [,3] [,4] [1,] 1 2 4 3 (cbind ((c (1, 1, 1)), (c (1, 0, 1)), (c (0, 1, 0)))) #=> 拼接矩陣 ## [,1] [,2] [,3] ## [1,] 1 1 0 ## [2,] 1 0 1 ## [3,] 1 1 0 ## =========== 矩陣線性代數 ## 矩陣轉置: 若是參數裏面只有一個參數時,而且是函數調用的時候,能夠省略參數標記的一對括號,以下=> (t (matrix ((c (1, 2, 1, 3, 5, 8)), ncol=2))) ## [,1] [,2] ## [1,] 1 3 ## [2,] 2 5 ## [3,] 1 8 ## ==>> ## [,1] [,2] [,3] ## [1,] 1 2 1 ## [2,] 3 5 8 ## 矩陣的標量運算 ('*' (10, (matrix ((c (1, 2, 1, 3, 5, 8)), ncol=2)))) ## [,1] [,2] ## [1,] 10 30 ## [2,] 20 50 ## [3,] 10 80 ## ## 矩陣求和: 必須結構相同才能相加 ('+' ((matrix ((c (9, 2, 3, 8, 1, 4)), ncol=2)), (matrix ((c (0, 3, 5, 3, 7, 2)), ncol=2)))) # A + B ## [,1] [,2] ## [1,] 9 8 ## [2,] 2 1 ## [3,] 3 4 ## [,1] [,2] ## [1,] 0 3 ## [2,] 3 7 ## [3,] 5 2 ## =======>>>>> ## [,1] [,2] ## [1,] 9 11 ## [2,] 5 8 ## [3,] 8 6 ## ## 矩陣乘法: A的列數必須等於B的行數 <=> 列的加權求和 ('%*%' ((matrix ((c (1, 4, 3, 0, 1, 2)), ncol=2)), (matrix ((c (7, 8)), ncol=1)))) # A * B ## [,1] ## [1,] 7 ## [2,] 36 ## [3,] 37