我們用上次做最後一道二項分布檢驗的數據。
library(dplyr)
testdata <- data.frame(ill=c(0,1,2,3),family=c(112,20,11,7))
testdata <- mutate(testdata, ill_number = ill*family)
p <- sum(testdata$ill_number)/(sum(testdata$family)*3)
x <- seq(0, 3,by =1)
testdata$P1 <- dbinom(x,3,p)
testdata <- mutate(testdata, theory_family = P1*family)
str(testdata)
## 'data.frame': 4 obs. of 5 variables:## $ ill : num 0 1 2 3## $ family : num 112 20 11 7## $ ill_number : num 0 20 22 21## $ P1 : num 0.63606 0.31063 0.05057 0.00274## $ theory_family: num 71.2383 6.2126 0.5562 0.0192
Syntax - Helpful conventions for wrangling使用tbl_df()函數,tbl’s are easier to examine than data frames
testdata_tbl_df <- tbl_df(testdata)
str(testdata_tbl_df)
## Classes 'tbl_df', 'tbl' and 'data.frame': 4 obs. of 5 variables:## $ ill : num 0 1 2 3## $ family : num 112 20 11 7## $ ill_number : num 0 20 22 21## $ P1 : num 0.63606 0.31063 0.05057 0.00274## $ theory_family: num 71.2383 6.2126 0.5562 0.0192
跟str()功能怎麼看起來一樣呢?
glimpse(testdata_tbl_df)
## Observations: 4## Variables: 5## $ ill <dbl> 0, 1, 2, 3## $ family <dbl> 112, 20, 11, 7## $ ill_number <dbl> 0, 20, 22, 21## $ P1 <dbl> 0.636056, 0.310632, 0.050568, 0.002744## $ theory_family <dbl> 71.238272, 6.212640, 0.556248, 0.019208
Reshaping Data - Change the layout of a data settestdata_tbl_df
## # A tibble: 4 x 5## ill family ill_number P1 theory_family## <dbl> <dbl> <dbl> <dbl> <dbl>## 1 0 112 0 0.636056 71.238272## 2 1 20 20 0.310632 6.212640## 3 2 11 22 0.050568 0.556248## 4 3 7 21 0.002744 0.019208
Gather columns into rows:
library(tidyr)
testdata2 <- gather(testdata_tbl_df, "taxon", "n", c(2,5))
## Warning in if (!is.finite(x)) return(FALSE): 條件的長度大於一,因此只能用其## 第一元素
testdata2
## # A tibble: 8 x 5## ill ill_number P1 taxon n## <dbl> <dbl> <dbl> <chr> <dbl>## 1 0 0 0.636056 family 112.000000## 2 1 20 0.310632 family 20.000000## 3 2 22 0.050568 family 11.000000## 4 3 21 0.002744 family 7.000000## 5 0 0 0.636056 theory_family 71.238272## 6 1 20 0.310632 theory_family 6.212640## 7 2 22 0.050568 theory_family 0.556248## 8 3 21 0.002744 theory_family 0.019208
前天我們是重新生成數據表的,效果是一樣的,但是如果數據量多,上面的方法就很方便幫我們做數據變形啦。
testdata3 <- data.frame(ill=testdata$ill,family=c(testdata$family,testdata$theory_family),txon=c("real","real","real","real","theory","theory","theory","theory"))testdata3
## ill family txon## 1 0 112.000000 real## 2 1 20.000000 real## 3 2 11.000000 real## 4 3 7.000000 real## 5 0 71.238272 theory## 6 1 6.212640 theory## 7 2 0.556248 theory## 8 3 0.019208 theory
Spread rows into columns:
spread(testdata3, key = txon, value = family)
## ill real theory## 1 0 112 71.238272## 2 1 20 6.212640## 3 2 11 0.556248## 4 3 7 0.019208
更多函數tidyr包separate() 一列按分隔符分割為多列
`unite()``將多列按指定分隔符合併為一列
dplry包select() 按照列名篩選列,可結合starts_with,ends_with,contains,matches,one_of,num_range和everything等使用
filter() 按照已定條件對行做過濾,類似標準函數subset
arrange() 對數據進行排序,類似標準函數order
summarise() 對數據進行匯總操作,可結合group_by使用,類似標註函數aggregate
join(),set(),distinct(),sample(),bind(),ifelse()
參考資料數據整合神器之tidyr包
數據整合神器之dplyr包
Data Wrangling with dplyr and tidyr Cheat Sheet
R語言學習筆記歷史記錄
基礎系列
R學習筆記-0-開始學習
R學習筆記-1-語法基礎
【資料庫學習筆記-2】R VS 資料庫
繪圖系列
用R繪製覆蓋深度的頻數直方圖
【R語言】韋恩圖的簡單繪製
數據分析系列
【學習筆記】宏基因組的差異基因分析-0
PANDA姐的轉錄組入門(7):差異基因分析-1
PANDA姐的轉錄組入門(8):差異基因結果注釋
統計學習系列
【好書分享】如何系統用R學習統計學?
【用R學統計】第一周統計練習-1
【用R學統計】第一周統計練習-2
【用R學統計】第一周統計練習-3
【用R學統計】第二周統計練習-1