學(xué)習(xí)小組day6筆記--思亮

學(xué)習(xí)R包

1.鏡像設(shè)置

file.edit('~/.Rprofile')
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))#清華鏡像
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")#中科大鏡像
options()$repos
options()$BioC_mirror
  1. Rstudio有兩個重要的配置文件:(Rstudio開啟運(yùn)行時先會查看配置文件)
  • .Renviron 設(shè)置R的環(huán)境變量
  • .Rprofile 一種代碼文件
  1. options函數(shù)用于設(shè)置R運(yùn)行過程中的一些選項(xiàng)設(shè)置

2.安裝

  1. make sure the reliability of your Internet
  2. install.packages(" packagesName") 安裝的包來自CRAN網(wǎng)站
  3. BiocManager::install("packages") 安裝的包來自Bioconductor

3. 加載

library(packagename)require(packagename

#安裝加載三部曲
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) 
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") 
install.packages("dplyr")
library(dplyr)
test <- iris[c(1:2,51:52,101:102),]

4.dplyr五個基本函數(shù)

  1. mutate(),新增列
mutate(test, New = c(1,2,3,4,5,6))
  1. select(),按列篩選
select(test,1) #根據(jù)索引篩選
select(test,Sepal.Length) #根據(jù)列名篩選
select(test,c(1,5)) #借用向量,按多個索引篩選
  1. filter(),按行篩選
filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5)
filter(test, Species %in% c("Setosa","versicolor"))
  1. arrange(),按某一列或某幾列對整個表格進(jìn)行排序
arrange(test, Sepal.Length)
arrange(test, desc(Sepal.Length))
  1. summarise(),匯總
summarise(test,mean(Sepal.Length),sd(Sepal.Length))
group_by(test,Species)
summarise(group_by(test,Species),mean(Sepal.Length),sd(Sepal.Length))

5.dplyr兩個實(shí)用功能

  1. 管道操作%>%(cmd/ctr + shift +M)
test %>% group_by(Species) %>% summarise(mean(Sepal.Length),sd(Sepal.Length))
  1. count統(tǒng)計(jì)某列的unique值
count(test,Species)

6.dplyr處理關(guān)系數(shù)據(jù)

  1. 內(nèi)連inner_join()
inner_join(test1, test2, by = "x") #取交集
  1. 左連left_join()
left_join(test2, test1, by = 'x') #以左側(cè)第一個數(shù)據(jù)集的x為標(biāo)準(zhǔn)
left_join(test1, test2, by = 'x') #注意兩者結(jié)果并不相同
  1. 全連full_join()
full_join(test1, test2, by = 'x') #取并集
  1. 半連接semi_join():
semi_join(test1, test2, by = 'x')
# 返回test1中有,test2中有,的test1中的元素
  1. 反鏈接(anti_join):
anti_join(test1, test2, by = 'x')
# 返回test1中有,test2中沒有沒有,的test1中的元素
  1. 簡單合并
bind_rows(test1, test2) #合并行要求列數(shù)相同
bind_cols(test1, test2)#合并列要求行數(shù)相同

7. 備注

  1. iris是內(nèi)置數(shù)據(jù)集
  2. 加載任意一個tidyverse包即可用管道函數(shù)
  3. base包中有簡單合并函數(shù)cbind() rbind()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容