安裝和加載R包
dplyr五個基礎(chǔ)函數(shù)
dplyr兩個實(shí)用技能
dplyr處理關(guān)系數(shù)據(jù)
安裝和加載R包
install.packages(“包”)
BiocManager::install(“包”) #安裝包
library(包)
require(包) #加載包
eg.
Error:
- Warning message:
程輯包‘dplyr’是用R版本4.1.3 來建造的
Solution:
先打開R的GUI,在進(jìn)行如下操作。
install.packages("installr")
library(installr)
updateR()
- The following objects are masked from ‘package:stats’:decompose, spectrum
解決方法:
library(igraph,warn.conflicts = F)
# 去除警告
https://blog.csdn.net/ouyangk1026/article/details/122165567
dplyr五個基礎(chǔ)函數(shù)
mutate(),新增列
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),]
mutate(test, new = Sepal.Length * Sepal.Width)
select(),按列篩選
select(test,1)
select(test,c(1,5))
select(test,Sepal.Length)
filter()篩選行
filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5 )
filter(test, Species %in% c("setosa","versicolor"))
arrange(),按某1列或某幾列對整個表格進(jìn)行排序
arrange(test, Sepal.Length)#默認(rèn)從小到大排序
arrange(test, desc(Sepal.Length))#用desc從大到小
summarise():匯總
summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 計(jì)算Sepal.Length的平均值和標(biāo)準(zhǔn)差
dplyr兩個實(shí)用技能
管道操作
%>% (cmd/ctr + shift + M)
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))