如同《Python一行文:if-else變形》,簡單的if-else判斷在R語言也可以寫成一行文,請見下列範例:
- x <- 5
- ifelse(x > 0, 'Positive', 'Negative or Zero')
[1] "Positive"
- x <- -1
- ifelse(x > 0, 'Positive', 'Negative or Zero')
[1] "Negative or Zero"
使用的語法:ifelse(test, yes, no)
- test:可以判斷是非的物件
- yes:當test測出來是TRUE,回傳的值
- no:當test測出來是FALSE,回傳的值
_EOF_
R的corrplot套件可將相關矩陣(correlation matrix)、信賴區間(confidence interval)圖形化,也能做矩陣重排(matrix reordering)
。下圖為一個correlation matrix:
上圖只要輸入程式碼:
- library(corrplot)
- M <- cor(mtcars)
- corrplot.mixed(M, lower="pie", upper="circle")
這位加拿大太空人叫Chris Hadfield,歌曲5m30s
改編自David Bowie's Space Oddity