學(xué)習(xí)小組Day06--R包學(xué)習(xí) by王宇鑫

鏡像設(shè)置

file.edit('~/.Rprofile')
把下面代碼加入配置文件中

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) 
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") 

查看

options()$repos
options()$BioC_mirror

>options()$repos
                                        CRAN 
"https://mirrors.tuna.tsinghua.edu.cn/CRAN/" 
> options()$BioC_mirror
[1] "https://mirrors.ustc.edu.cn/bioc/"

數(shù)據(jù)框操作R包 dplyr

  • 此包為數(shù)據(jù)框的操作提供了許多便捷的操作
    install.packages("dplyr")

dplyr provides a flexible grammar of data manipulation

library(dplyr)
test <- iris[c(1:2,51:52,101:102),]
#加一列
test$new=test$Sepal.Length*test$Sepal.Width
mutate(test,t=Sepal.Length*Sepal.Width)
#按列
select(test,1)
head(test[,1])
head(test[,c(2,3)])
select(test,Petal.Width)
test[,c("Petal.Width")]
head(test[,colnames(test)=="Petal.Width"])
#按行
filter(test,Species == "setosa")
filter(test,Species == "setosa"&Sepal.Length > 5)
filter(test,Species %in% c("setosa","versicolor"))
#arrange排序
arrange(test,Sepal.Length)
arrange(test,desc(Sepal.Length))
#匯總
summarise(test,mean(Sepal.Length))
group_by(test,Species)
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
##實(shí)用技能
#計(jì)算unique值
count(test,Species)
#處理關(guān)系數(shù)據(jù)
test1 <- data.frame(x = c('b','e','f','x'), 
                    z = c("A","B","C",'D'),
                    stringsAsFactors = F)
test2 <- data.frame(x = c('a','b','c','d','e','f'), 
                    y = c(1,2,3,4,5,6),
                    stringsAsFactors = F)
#半連接,返回與Y匹配的所有x
semi_join(x = test1, y = test2, by = 'x')
#全連接
full_join( test1, test2, by = 'x')
#內(nèi)連接:交集
inner_join(test1, test2, by = "x")
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容