R包
一、R studio預(yù)設(shè)鏡像
1.設(shè)置
file.edit('~/.Rprofile')
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #對應(yīng)清華源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #對應(yīng)中科大源
2.查看設(shè)置
options()$repos
options()$BioC_mirror
聽說設(shè)置是否成功全靠緣分芦缰,我可能只有一半緣分
image.png
二企巢、dplyr包——計(jì)算,數(shù)據(jù)整理
1.添加列
mutate(test, new = Sepal.Length * Sepal.Width)
2.篩選
#按列號篩選
select(test,1)
select(test,c(1,5))
select(test,Sepal.Length)
#按列名篩選
select(test, Petal.Length, Petal.Width)
vars <- c("Petal.Length", "Petal.Width")#var是"Petal.Length", "Petal.Width"
select(test, one_of(vars))
#用filter篩選行
filter(test, Species == "setosa")#在test中篩選spcies值未setosa的行
filter(test, Species == "setosa"&Sepal.Length > 5 )
#a %in%b让蕾,取a在b中的值
filter(test, Species %in% c("setosa","versicolor"))
3.排序
arrange(test, Sepal.Length)#默認(rèn)從小到大排序
arrange(test, desc(Sepal.Length))#用desc從大到小
4.匯總浪规,分組
summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 計(jì)算Sepal.Length的平均值和標(biāo)準(zhǔn)差
# 先按照Species分組,計(jì)算每組Sepal.Length的平均值和標(biāo)準(zhǔn)差
group_by(test, Species)
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
三探孝、管道操作
#管道操作%>% (cmd/ctr + shift + M)
#%>% 直接把數(shù)據(jù)傳遞給下一個公式笋婿,任意一個tidyverse包可用
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
count(test,Species)
四、關(guān)聯(lián)數(shù)據(jù)
操作關(guān)系數(shù)據(jù)不要引入factor
options(stringsAsFactors = F)
1.連接
#內(nèi)連inner顿颅,取交集
inner_join(test1, test2, by = "x")
#左連left
left_join(test1, test2, by = 'x')
left_join(test2, test1, by = 'x')
#全連full
full_join( test1, test2, by = 'x')
#半連接:返回能夠與y表匹配的x表所有記錄
semi_join(x = test1, y = test2, by = 'x')
#反連接:返回?zé)o法與y表匹配的x表的所記錄
anti_join(x = test2, y = test1, by = 'x')
2.合并
bind_rows()
bind_cols()
今日小結(jié)
image.png