Day6

R包

1 安裝

1.1 鏡像設(shè)置

tools/global options/package

1.2 安裝命令

install.packages('包')
biocManager::install('包')
#取決于你要安裝的包存在于CRAN網(wǎng)站還是Biocductor
library(包)
require(包)
#打開包

2 dplyr五個基礎(chǔ)函數(shù)

test <- iris[c(1:2,51:52,101:102),]
#取出iris數(shù)據(jù)中第1,2,51,52,101,102行的數(shù)據(jù)

2.1 mutate 添加列

mutate(test, new = Sepal.Length * Sepal.Width)
#新建列,數(shù)據(jù)從test中取,列名為new,數(shù)據(jù)為列Sepal.Length 和Sepal.Width相乘的結(jié)果

注意,這里添加的列的不存在于test中的,如果需要改變的話需要賦值給test(test<-)

2.2 select 篩選列

按列號篩選
select(test,c(1,5))
取出第一和第五列
按列名篩選
select(test, Petal.Length, Petal.Width)
取出名叫Petal.Length, Petal.Width的兩列

2.3 filter 篩選行

filter(test, Species == "setosa")
#篩選test變量中Species這一列中是setosa的行
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5                  1.4                  0.2        setosa
## 2          4.9         3.0                  1.4                  0.2        setosa
filter(test, Species == "setosa"&Sepal.Length > 5 )
#篩選test變量Species這一列中是setosa并且Sepal.Length>5的行
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5                   1.4                  0.2        setosa
filter(test, Species %in% c("setosa","versicolor"))
#篩選物種存在于向量c("setosa","versicolor")中的行

2.4 arrange 按某1列或某幾列對整個表格進行排序

arrange(test, Sepal.Length)#默認從小到大排序
arrange(test, desc(Sepal.Length))#desc:降序排列

2.5 summarise 匯總

group_by(test,species) 
#對于test變量中的species列進行分組,相同的放在一起
summarise(test,mean(Sepal.Length),sd(Sepal.Width))
#分別計算test變量中Sepal.Length和Sepal.Width列的平均值和方差
summarise(group_by(test,Species),mean(Sepal.Length),sd(Sepal.Width))
#分別計算test變量不同物種中Sepal.Length和Sepal.Width列的平均值和方差、

3 dplyr的實用技能

3.1 管道操作

%>% 用ctrl+shift+M可以打出來

test %>%
  group_by(Species) %>% 
  summarise(mean(Sepal.Length),sd(Sepal.Length))
#這段的功能和summarise(group_by(test,Species),mean(Sepal.Length),sd(Sepal.Width))是一樣的,意思是可以直接把數(shù)據(jù)傳遞給下一個函數(shù)調(diào)用或表達式

3.2 count統(tǒng)計某列的unique值

count(test,Species) #統(tǒng)計Species中的種類以及數(shù)量

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

新建數(shù)據(jù)框

test1 <- data.frame(x=c('1','2','3','4'),
                    y=c('a','b','c','d'),
                    stringsAsFactors=T)
#stringsAsFactors: 邏輯:字符向量是否應(yīng)該轉(zhuǎn)換為因子?默認值是TRUE,但是可以通過設(shè)置選項(stringsAsFactors=FALSE)來改變這一點。

1.內(nèi)連inner_join,取交集
inner_join(test1, test2, by = "x")
2.左連left_join(從test1里取x列的值,并把對應(yīng)的y表的數(shù)據(jù)加上)
left_join(test1, test2, by = 'x')
3.全連full_join
full_join( test1, test2, by = 'x')
4.半連接:返回能夠與y表匹配的x表所有記錄semi_join
semi_join(x = test1, y = test2, by = 'x')
5.反連接:返回?zé)o法與y表匹配的x表的所記錄anti_join
anti_join(x = test2, y = test1, by = 'x')
6.簡單合并
bind_rows(test1, test2) #把test1和test2中的行合并,需要兩個表格列數(shù)相同
bind_cols(test1, test3) #把test1和test2中的列合并,需要有相同的行數(shù)
相當(dāng)于base包里的cbind()函數(shù)和rbind()函數(shù)

?著作權(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)容