R語言package使用
1. 包的安裝和使用
image.png
install.packages()
放接,會自動安裝運行包所需的環(huán)境library()
激活包的環(huán)境
2. 實例 dpylr
dplyr is a grammar of data manipulation, providing a consistent set of verbs that help you solve the most common data manipulation challenges
參考網(wǎng)站:dplyr
dolyr安裝和激活
browseVignettes(package = "dplyr")
library(dplyr)
用內(nèi)部數(shù)據(jù)框iris創(chuàng)建新的數(shù)據(jù)框并處理數(shù)據(jù)
test <- iris[c(1:2,51:52,101:102),]
mutate(test,new=Sepal.Length*Sepal.Width) #新增列
select(test) #按列篩選
filter(test,Species=="setosa")
filter(test,Species=="setosa"&Sepal.Length>5) #合并兩個篩選條件
filter(test,Species %in% c("versicolor","virginica"))
arrange(test,Sepal.Width) #從小到大排列
arrange(test,desc(Sepal.Width)) #從大到小排列
對數(shù)據(jù)進行統(tǒng)計學分析
summarise(test,median(Sepal.Width),sd(Sepal.Width)) #數(shù)據(jù)統(tǒng)計匯總,有多種統(tǒng)計學函數(shù)芜赌,詳見F1
group_by(test,Species) #根據(jù)某一變量分組
summarise(group_by(test,Species),median(Sepal.Width),sd(Sepal.Width))
一個重要的功能:管道函數(shù)%>%
%>% #control+shift+M 管道函數(shù)啥么,左邊的對象傳遞給右邊,類似于賦值
對幾個數(shù)據(jù)框的處理
x<-seq(1:4)
y<-c("a","b","c","d")
test1<-data.frame(x,y,stringsAsFactors = F)
test2<-data.frame(x=seq(2:5),y=c("a","b","d","f"),stringsAsFactors = F)
inner_join(test1,test2) #取交集
left_join(test2,test1,by="x") #左連(前面的那個數(shù)據(jù)框)
full_join(test1,test2,by="x") #合并兩個數(shù)據(jù)框
semi_join(test1,test2,by="x") #返回能夠與y表匹配的x表所有記錄
anti_join(test1,test2,by="x") #返回返回無法與y表匹配的x表的所記錄