R包學(xué)習(xí)
安裝與加載R包
配置鏡像
-
臨時(shí)鏡像設(shè)置
-
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
-
-
永久設(shè)置
file.edit('~/.Rprofile')
-
檢查鏡像
options()$repos和options()$BioC_mirror
安裝
install.packages(“包”)或BiocManager::install(“包”)
加載
-library()
安裝dplyr包
install.packages("dplyr")
dplyr5個(gè)基本函數(shù)
新增列
mutate(test, new = Sepal.Length * Sepal.Width)
按列篩選
-
按列號(hào)篩選
select(test,1)翔悠,select(test,c(1,5))厂汗,
-
按列名篩選
-
select(test, Petal.Length, Petal.Width)委粉,vars <- c("Petal.Length", "Petal.Width")
select(test, one_of(vars))
-
按行篩選
filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5 )
filter(test, Species %in% c("setosa","versicolor"))
按列排序
arrange(test, Sepal.Length)#默認(rèn)從小到大排序,arrange(test, desc(Sepal.Length))#用desc從大到小
匯總
-
直接匯總
summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 計(jì)算Sepal.Length的平均值和標(biāo)準(zhǔn)差
-
使用group_by()再匯總
group_by(test, Species)娶桦,summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
dplyr2個(gè)實(shí)用技能
管道操作%>%
group_by(Species) %>% summarise(mean(Sepal.Length), sd(Sepal.Length))```
count統(tǒng)計(jì)
count(test,Species)
dplyr處理關(guān)系數(shù)據(jù)
內(nèi)連取交集inner_join
inner_join(test1, test2, by = "x")
左連left_join
left_join(test1, test2, by = 'x')
全連full_join
full_join( test1, test2, by = 'x')
半連接:返回能夠與y表匹配的x表所有記錄semi_join
semi_join(x = test1, y = test2, by = 'x')
反連接:返回?zé)o法與y表匹配的x表的所記錄anti_join
anti_join(x = test2, y = test1, by = 'x')
簡(jiǎn)單合并
-
按行合并
bind_rows(test1, test2)
-
按列合并
bind_cols(test1, test3)