學(xué)習(xí)小組Day6筆記--lxl

思維導(dǎo)圖

R包安裝.PNG

配置鏡像

參考生信星球https://mp.weixin.qq.com/s/XvKb5FjAGM6gYsxTw3tcWw

初級模式

初始配置 生信星球.PNG

缺點:這個是CRAN的鏡像,如果要下載Bioconductor的包,這個鏡像是沒有辦法用的。

升級模式

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #對應(yīng)清華源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #對應(yīng)中科大源

缺點:下次再打開Rstudio會發(fā)現(xiàn),下載Bioconductor還是會回到官方鏡像。可以查詢options()$BioC_mirror試試,如果你的依然是自己設(shè)置的國內(nèi)鏡像,就不用管了;如果發(fā)現(xiàn)需要再重新運行一遍代碼進行設(shè)置。

一勞永逸的高級模式

file.edit('~/.Rprofile')
然后在其中添加好上面的options代碼,保存,重啟Rstudio

安裝包

`install.packages(“包”)`
`BiocManager::install(“包”)`

加載包

`library(包)`
`require(包)`

R包的講解(dplyr為例)

dplyr包,d=data.frame,是專門用于數(shù)據(jù)框的

dplyr基礎(chǔ)函數(shù)

示例數(shù)據(jù)

test <- iris[c(1:2,51:52,101:102),]
  • mutate(data, newcolumn= ),新增列
mutate(test, new = Sepal.Length * Sepal.Width)
  • select(data, colnumber/colname),篩選列
select(test,c(1,5))
select(test, Petal.Length, Petal.Width)
vars <- c("Petal.Length", "Petal.Width")
select(test, one_of(vars))
  • filter(data, 需要滿足的條件(常常是某一列的內(nèi)容滿足什么)),篩選行
filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5 )
filter(test, Species %in% c("setosa","versicolor"))
  • arrange(data, colname),按某1列或某幾列對整個表格進行排序
    默認是從小到打排序,arrange(data, desc(colname))從大到小排序
arrange(test, Sepal.Length)
arrange(test, desc(Sepal.Length))
  • summarise():匯總
    summarise(group_by(), ),結(jié)合group_by使用實用性強
summarise(test, mean(Sepal.Length), sd(Sepal.Length))
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))

實用技能

  • 管道操作 %>% (cmd/ctr + shift + M)
    (加載任意一個tidyverse包即可用管道符號)
test %>% 
  group_by(Species) %>% 
  summarise(mean(Sepal.Length), sd(Sepal.Length))
  • count(data, colnames) 統(tǒng)計某列的unique值
count(test,Species)

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

示例數(shù)據(jù)

options(stringsAsFactors = F) #注意:不要引入factor
test1 <- data.frame(x = c('b','e','f','x'), 
                    z = c("A","B","C",'D'),
                    stringsAsFactors = F)
test2 <- data.frame(x = c('a','b','c','d','e','f'), 
                    y = c(1,2,3,4,5,6),
                    stringsAsFactors = F)
  • inner_join(data1,data2, by=哪一列)
    內(nèi)連取交集,大家共有的才合并
inner_join(test1, test2, by = "x")
  • left_join(data1,data2,by=哪一列)
    左連,第一個數(shù)據(jù)集有的在第二個數(shù)據(jù)集里面找然后合并,沒有匹配的就空值NA
left_join(test1, test2, by = 'x')
left_join(test2, test1, by = 'x')
  • full_join(data1,data2,by=哪一列)
    全連取并集,沒有匹配就空值NA
full_join( test1, test2, by = 'x')
  • semi_join(x, y , by =哪一列)
    半連接:返回能夠與y表匹配的x表所有記錄,不合并
semi_join(x = test1, y = test2, by = 'x')
  • anti_join(x , y , by =哪一列)
    反連接:返回?zé)o法與y表匹配的x表的所記錄,不合并
anti_join(x = test2, y = test1, by = 'x')
  • bind_rows(), bind_cols()簡單合并
    bind_rows()函數(shù)需要兩個表格列數(shù)相同,bind_cols()函數(shù)則需要兩個數(shù)據(jù)框有相同的行數(shù)
test1 <- data.frame(x = c(1,2,3,4), y = c(10,20,30,40))
test2 <- data.frame(x = c(5,6), y = c(50,60))
test3 <- data.frame(z = c(100,200,300,400))
bind_rows(test1, test2)
bind_cols(test1, test3)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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