R語(yǔ)言-如何按照某一列分組求均值
主要介紹tapply函數(shù):
每次只能求一列
aggregate函數(shù):每次按組可以求多列
tapply(shuju[shuju[,3],shuju$year,mean)
以年份為組,求shuju表第三列的均值
aggregate(shuju[,3:4],list(shuju[,2]),mean)
以年份為均值,求數(shù)據(jù)表第三列,第四列的均值
補(bǔ)充:R語(yǔ)言按某一列分類(lèi)求均值+繪圖總結(jié)
看代碼吧~
D<-aggregate(.~K,data=data1,mean) #求數(shù)據(jù)集data1按照K分類(lèi)后所有列的均值
rm(list=ls()) #刪除所有對(duì)象
attach() #鎖定某個(gè)對(duì)象
with(mtcars,{print(summary(mpg)),plot(mpg,disp)} #with作用等同attach
grades<-read.table('student.csv',header=TRUE,row.namens='studentid',sep=',')
#讀表
dev.new() #開(kāi)啟新圖框
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)
畫(huà)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)
畫(huà)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語(yǔ)言控制結(jié)構(gòu)知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理一篇關(guān)于R語(yǔ)言控制結(jié)構(gòu)知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-03-03
R語(yǔ)言 Factor類(lèi)型的變量使用說(shuō)明
這篇文章主要介紹了R語(yǔ)言 Factor類(lèi)型的變量使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Rcpp和RcppArmadillo創(chuàng)建R語(yǔ)言包的實(shí)現(xiàn)方式
這篇文章主要為大家介紹了Rcpp和RcppArmadillo創(chuàng)建R包實(shí)現(xiàn)方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11

