R語言-如何按照某一列分組求均值
主要介紹tapply函數(shù):
每次只能求一列
aggregate函數(shù):每次按組可以求多列
tapply(shuju[shuju[,3],shuju$year,mean)
以年份為組,求shuju表第三列的均值
aggregate(shuju[,3:4],list(shuju[,2]),mean)
以年份為均值,求數(shù)據(jù)表第三列,第四列的均值
補(bǔ)充:R語言按某一列分類求均值+繪圖總結(jié)
看代碼吧~
D<-aggregate(.~K,data=data1,mean) #求數(shù)據(jù)集data1按照K分類后所有列的均值 rm(list=ls()) #刪除所有對象 attach() #鎖定某個(gè)對象 with(mtcars,{print(summary(mpg)),plot(mpg,disp)} #with作用等同attach grades<-read.table('student.csv',header=TRUE,row.namens='studentid',sep=',') #讀表 dev.new() #開啟新圖框 dev.off() #關(guān)閉圖框
dose<-c(20,30,40,50,60) drugA<-c(16,20,25,35,42) drugB<-c(20,35,46,61,70) opar<-par(no.readonlyTRUE) par(pin=c(2,3)) #圖片尺寸 par(cex.axis=.75,font.axis=3) par(lwd=2,cex=1.5) plot(dose,drugA,type='b',pch=19,lty=2,col='red') plot(dose,drug,type='b',pch=23,lty=6,col='blue',bg='green') par(opar)
plot(dose,drugA,type='b',col='red', lty=2,pch=2,lwd=2,main='clain', sub='this is',xlab='dosa',ylab='drug', xlim=c(0,60),ylim=c(0,70))
圖例
#legend(location,title,legend) dose<-c(20,30,40,50,60) drugA<-seq(1,10,2) drugB<-seq(2,20,2) opar<-par(no.readonly=TRUE) par(lwd=2,cex=1.5,font.lab=2) plot(dose,drugA,type='b',pch=15,lty=1, col='blue',ylim=c(0,60),main='that', xlab='drug',ylab='resopme') lines(dose,drugB,type='b',pch=17,lty=2,col='blue') legend('topleft',inset=0.05,title='main',c('A','B'),lty=c(1,2), pch=c(15,17),col=c('red','blue')) par(opar)
畫2*2圖:
attach(mtcars) opar<-par(no.readonly=TRUE) par(mfrow=c(2,2)) plot(wt,mpg,main='11') plot(wt,disp,main='xx') hist(wt,main='dd') boxplot(wt,main='ds') par(opar) detach(mtcars)
畫3*1圖:
attach(mtacars) opar<-par(no.readonly=TRUE) par(mfrow=c(3,1)) hist(wt) hist(disp) hist(mpg) par(opar) detach(mtcars)
第一幅圖在第一行,第二三副圖在第二行:
attach(mtcars) opar<-par(no.readonly=TRUE) layout(matrix(c(1,1,2,3)2,2,byrow=TRUE)) hist(wt) hist(mpg) hist(disp) detach(macars)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
R語言讀取txt文件中的內(nèi)容實(shí)現(xiàn)
R語言提供了多種讀取文本數(shù)據(jù)的函數(shù),本文主要介紹了R語言讀取txt文件中的內(nèi)容實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03R語言實(shí)現(xiàn)對數(shù)據(jù)框按某一列分組求組內(nèi)平均值
這篇文章主要介紹了R語言實(shí)現(xiàn)對數(shù)據(jù)框按某一列分組求組內(nèi)平均值,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03使用R語言繪制散點(diǎn)圖結(jié)合邊際分布圖教程
這篇文章主要介紹了使用R語言利用ggplot繪制散點(diǎn)圖,并且在圖像的兩邊繪制邊際分布圖(包括邊際直方圖與邊際密度函數(shù))我們這里介紹兩種方法進(jìn)行繪制2021-11-11詳解R語言caret包trainControl函數(shù)
這篇文章主要介紹了R語言caret包trainControl函數(shù)詳解,本文通過源碼分析給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08