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

2017年4月28日 星期五

melt() 使用時 measure variable 的型態要一致

使用 melt() 來 reshape 資料時, measure variable 的型態要一致,否則會出現警告。

例如有以下的資料:


資料中,subject 欄位為 factor。若使用 subject 為 id.var 進行 melt,則會出現以下警告訊息:


因為,第一個欄位為 Factor 型態,melt() 把後面其他的欄位也視為 Factor,造成轉換時的錯誤。


Reference:
[1] thiagogm, 2013. Reshape and aggregate data with the R package reshape2, www.r-bloggers.com.

2017年3月17日 星期五

在 RStudio 啟動時自動執行語系切換 指令


問題

在資料中有中文字,每次進 RStudio 時都要手動執行 Sys.setlocale(category="LC_ALL", locale = "cht") 將語系改成  CP-950。

做法

在專案目錄下新增一個 .Rprofile 的檔案,加入 Sys.setlocale(category="LC_ALL", locale = "cht")  指令。如此,在開啟專案時便會自動執行 .Rprofile 內的指令[1]。


參考資料

[1]  Josh Paulson, 2017/1/22, Using Projects, https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects, Accessed on 2017/3/18

2016年7月14日 星期四

R 相關參考資料


資料視覺化

[1] Teutonico, D., ggplot2 Essentials, Packt Publishing, 2015. (Comment: ggplot2 的入門書,對  grammar of graphics 有清楚的解釋)[Packt Publishing][天瓏]
[2]  Data Visualization with ggplot2: Cheat Sheet, R Studio. (ggplot2 的 cheat sheet, 讓你一目了然)
[3] Data Visualization: When To Use Which Graph (介紹各種圖形的使用時機)
[4] 資料視覺化 (整理、搜集及實驗各式各樣的資料視覺化 / 資訊圖表以及他們的相關資訊,以中文的方式提供給華人社群)
[5] ggplot 官方網站 (http://ggplot.yhathq.com/) 提供 ggplot 的使用說明, 非常的詳盡.  (*****)
[6] ggplot2 官方網站
[7] Cookbook for R
[8] Trina Chiasson、Dyanna Gregory, 資料準備與視覺化的簡單入門.
[9] Scales, axes and legends  介紹 ggplot2 中的 Scale, Axes 及  Legends 的觀念及實作.

統計

(介紹資料非常態分配時的原因及不需非常態分配的檢定工具)
[2] Power of a hypothesis test, University of Melbourne (想要知道樣本數及檢定效果如何影響檢定的 power 嗎? 這個網站提供了動畫, 讓你動手試試看)


Data Cleaning

[1] Erhard Rahm∗ Hong Hai Do, Data Cleaning: Problems and Current Approaches.
[2] Edwin de Jonge, Mark van der Loo, 2013.  An introduction to data cleaning with R, Statistics Netherlands.
[3] 劉正山、莊文忠, 2013. 項目無反應資料的多重插補分析,  臺灣選舉與民主化調查(TEDS)方法論之回顧與前瞻 .

Data Mining

[1] Daniel T. Larose And Chantal D. Larose, Data mining and predictive analytics,  2nd edition, John Wiley & Sons, 2015 

2016年5月12日 星期四

使用 R 匯入 Excel 存成的 CSV 檔, 在 Windows 環境下

Excel 的內容存成 CSV 檔時, 若在 Windows 環境下會存成 Big5 (CP-950)編碼。

使用 read.table() 指令讀取 CSV 檔時,若 R 的 coding page 不是 CP-950, 則匯入的中文字會編成亂碼。

使用 l10_info() 指令來看 R 的 coding page.





若不是 CP-950, 使用指令  Sys.setlocale(category="LC_ALL", locale = "cht") 切換。


讀入 CVS 讀時, 不要再加 encoding="UTF-8" 選項。雖然我們在 R-Studio 中設定存 Default Text Encoding 選項為 UTF-8,這個選項是指存檔時的編碼。在 R 中還是使用 CP-950 編碼。



延伸閱讀





2016年3月2日 星期三

data.frame 中的 row 及 column 操作, 使用 dplyr 套件


Row Operations

Filtering and slicing rows

slice()
filter()

Sorting rows

arrange()

Select distinct rows

distinct()

Column Operations

Column Selecting and rename

select()

Add new column

mutate()

Column-wise descriptive statistics

summarise()
注意 column name 的英文大小寫有別

 #對 iris 中的萼片長度計算平均值及變異數  
 data("iris")  
 summarise(iris, mean.SL=mean(Sepal.Length), sd.SL = sd(Sepal.Length))  

結果:


Group-wise + Column-wise descriptive statistics

group_by() + summarise()
使用 group_by() 會產生另外一個 data.frame, 其中會包含 grouping 的資訊. 
這個分群後的 data.frame 進到 summarise() 函數時, summarise()會看到 data.frame 中的分群資訊, 自動會去計算分群後 各 column 的統計量. 使用 str() 可以看到 data.frame 中的分群資訊。

範例:
 # 對 iris 以 species 欄位進行分群, 再計算每群的萼片長度計算平均值及變異數  
 iris.grouped <- group_by(iris, Species)  
 summarise(iris.grouped, count=n(), mean.SL=mean(Sepal.Length), sd.SL=sd(Sepal.Length))  

結果:

參考資料:

Jaynal Abedin and Kishor Kumar Das, Data Manipulation with R, 2nd, Packt Publising, 2015

2016年2月3日 星期三

匯入 UTF-8 編碼的中文檔案並在 R-Studion Console中顯示中文字

環境: Win7, RStudio Version 0.99.491

原理:
1. 先在 Single Byte Character Set 環境中讀入 UTF-8 編碼的中文字
2. 再轉成 Double Byte Character Set 環境顯示內容

1:  setwd("d:/R-Workshop/CaseStudy")  
2:  # Change to single byte character set to read UTF-8 encoded characters  
3:  Sys.setlocale("LC_CTYPE", "us")  
4:  l10n_info()  
5:  # Read the csv file encoded with UTF-8   
6:  my.data <- read.csv(file="UV_20151116152215.csv", header = TRUE, encoding="UTF-8")  
7:  # Show content with the single byte character set  
8:  # The RStudio Console cannot display the Chinese characters correctly in   
9:  # the Single-Byte-Character-Set environment.  
10:  head(my.data)  
11:  # Change to Double-Byte-Character-Set environment  
12:  Sys.setlocale("LC_CTYPE", "cht")  
13:  # Now your console can display Chinese characters correctly.  
14:  head(my.data)  
15:  rm(my.data)