Day6-學習R包.png
今天學習了R包dplyr,總結一下就是需要學會 :
- 設置鏡像
- 鏡像的高級設置方法(不用每次打開Rstudio都得運行設置鏡像代碼,其實是將程序?qū)戇M了Rstudio的開啟運行程序中蟹略,_)
- 然后是 安裝R包(CRAN來源的R包和Bioconductor來源的R包加載方式不一樣) 加載R包
- 重點是dplyr包
1.可以先設置全局變量options(stringsAsFactors = F)
避免將factor引入數(shù)據(jù)框枝恋。
2.后依次學習了5個基礎函數(shù)(mutate()段标、select()沃但、filter()、arrange()拣挪、summarise()),其中summarise()可以和group_by()連用效果很好擦酌。 - dplyr包的兩個實用技能
- 管道操作 %>% (cmd/ctr + shift + M) #加載任意一個tidyverse包即可用管道符號
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
- count統(tǒng)計某列的unique值
count(test,Species)
## # A tibble: 3 x 2
## Species n
##
## 1 setosa 2
## 2 versicolor 2
## 3 virginica 2
- dplyr包處理關系型數(shù)據(jù)的5個函數(shù)
- 內(nèi)連inner_join,取交集
inner_join(test1, test2, by = "x")
- 左連left_join,取交集
left_join(test1, test2, by = 'x')
- 全連full_join
full_join( test1, test2, by = 'x')
- 半連接:返回能夠與y表匹配的x表所有記錄semi_join
semi_join(x = test1, y = test2, by = 'x')
- 反連接:返回無法與y表匹配的x表的所記錄anti_join
anti_join(x = test2, y = test1, by = 'x')
- 在相當于base包里的cbind()函數(shù)和rbind()函數(shù);注意,
bind_rows()函數(shù)需要兩個表格列數(shù)相同菠劝,而bind_cols()函數(shù)則需要兩個數(shù)據(jù)框有相同的行數(shù)
bind_rows(test1, test2)
bind_cols(test1, test3)