熱門文章

顯示具有 R 標籤的文章。 顯示所有文章
顯示具有 R 標籤的文章。 顯示所有文章

2020年3月5日 星期四

R_讀檔遇到中文路徑怎辦

R_讀檔遇到中文路徑怎辦
環境:windows10
情境:使用readr::read_csv 無法讀取 中文路徑,建議改用 read.table

# -----------readr::read_csv 無法讀取 中文路徑
# readcsv <- function(filename) {
#   library(readr)
#   rescsv <- read_csv(filename,
#                      locale = locale(encoding = "BIG5"))
#   return(rescsv)
# }
# -------------改用 read.table
readcsv <- function(filename){
  rescsv <- read.table(filename, header=TRUE, 
           sep=",", stringsAsFactors=FALSE)
  return(rescsv)
}
filenames
# ----------- processing
data.list <- lapply(filenames, readcsv)
data.df <- do.call("rbind", data.list)

2018年8月5日 星期日

R【quantmod】101

介紹quantmod

quantmod

quantmod 是 R 的財務分析熱門套件,可以使用一個函數就下載來自 yahoo finance, google finance 的財務資料,同時可以繪製精美的 K 線圖、進行回測等功能。

TTR

TTR ( Technical Trading Rules )為 R 的 量化分析套件,安裝quantmod 時也會一併安裝。
其包含大多數的技術分析指標,例如:


addSMA() #移動平均線
addBBands() #布靈通道
addBBands(draw="p") #布靈通道 Bollinger%b
addMACD() #指數平滑移動平均線 Moving Average Convergence / Divergence
addRSI() #相對強弱指數 Relative Strength Index

不過因為是免費資料,所以有時候可能會有資料缺漏的狀況,可搭配na.omit將NA值移除

範例

library(quantmod)
LARGAN <- getSymbols("3008.TW", from="2010-1-1")
LARGAN <- na.omit('LARGAN') # remove the NA Value
dim('LARGAN')
summary('LARGAN')
head('LARGAN')
#get Chart
chartSeries('LARGAN')
#change theme likely TWstock
chartSeries(to.monthly(LARGAN),theme="white",up.col = "red",dn.col = "green")

# add TTR
addSMA()
addSMA(60)
addBBands() #布林通道
addMACD()
addRSI()

錯誤排除

when error "invalid graphics state" appear
1. dev.off()
2. Restart R
   Ctrl+Shift+F10
3. install.packages("quantmod")  # re install

2018年4月25日 星期三

R 101_安裝 and 安裝舊版既有套件

Step01.至 https://cran.r-project.org/

Step02.選擇Download R for Windows

Step03.選擇base


 Step04.點選 Download R X.X.X(版本) for Windows
 Step05.將舊版R套件安裝至新版

  •  先進入舊版的 R,執行
oldPkgs = installed.packages()
oldPkgs = oldPkgs[,"Package"]
save(oldPkgs,file="d:/oldPkgs.Rdata")

  • 進入新版 R 3.5.0,執行
# 扣除新版已經安裝的基本套件
pkgDiff = setdiff(oldPkgs,basePkg)
install.packages(pkgDiff)

oldPkgs = installed.packages()
oldPkgs = oldPkgs[,"Package"]
save(oldPkgs,file="d:/oldPkgs.Rdata")

-----------------------------
 Ref:台灣R軟體Club https://www.facebook.com/groups/1210634969026548/

2018年1月6日 星期六

Website Log Analysis with R



function-------------------------------------

dplyr::mutate+ifelse+grepl

dplyr::group_by+summarise


ggplot2::date_minor_breaks
標籤 xlab(), ylab(), labs(x=,y=), ggtitle()
每一個geom_*()都有參數可設定
圖形樣式設定 theme(),可使用內建樣式
theme_gray(): 灰背景,預設樣式
theme_bw(): 黑白樣式
使用其他樣式套件
ggthemes packages Website
xkcd packages Website





Ref-------------------------------------------
dplyr

https://rstudio-pubs-static.s3.amazonaws.com/116317_e6922e81e72e4e3f83995485ce686c14.html#/9
datetime format in R

https://www.stat.berkeley.edu/~s133/dates.html
ggplot2

http://ggplot2.tidyverse.org/reference/scale_date.html

https://github.com/yijutseng/CGMH_R/blob/master/03Visualization.md
Visualization

https://blog.hubspot.com/marketing/data-visualization-choosing-chart

https://cdn2.hubspot.net/hub/53/file-863940581-pdf/Data_Visualization_101_How_to_Design_Charts_and_Graphs.pdf