讀書筆記之 資料框操作與常用函式
阿新 • • 發佈:2019-01-02
如何把列表轉化為整齊的矩陣?
mylist<-as.list(iris[,1:4])
result<-sapply(mylist,mean)
result<-lapply(mylist,mean)
#結果
【1】Sepal.Length Sepal.Width Petal.Length Petal.Width
5.843333 3.057333 3.758000 1.199333
【2】 $Sepal.Length
[1] 5.843333
$Sepal.Width
[1] 3.057333
$Petal.Length
[1] 3.758
$Petal.Width
[1] 1.199333
由上可以發現兩個的輸出型別是不一樣的。【1】是向量,【2】是list.
將list轉化為matrix的操作:
#第一種是先轉成資料框,再轉置
t(as.data.frame(result))
class(A)
[1] "matrix"
#第二種是使用取子集的二元操作符作為sapply的引數
t(sapply(result,'['))
#將result傳入rbind
do.call('rbind',result)
class(C)
[1] "matrix"
常用的函式:
1.tapply()用法:
tapply(X, INDEX, FUN = NULL , ..., default = NA, simplify = TRUE)
#INDEX 為因子(分類變數)
例:
tapply(X=iris$Sepal.Length,INDEX=list(iris$Species),FUN=mean)
#結果
setosa versicolor virginica
5.006 5.936 6.588
2.類似的函aggregate()
aggregate(x, by, FUN, ..., simplify = TRUE, drop = TRUE)
#x為資料框
例子用這兩個函式簡寫就是:
with(iris,tapply(Sepal.Length,list(Species) ,mean))
with(iris,aggregate(Sepal.Length,by=list(Species),mean))
#僅輸出資料結構不一樣,aggregate更加友好,為資料框
3.mapply()函式
mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE,USE.NAMES = TRUE)
#兩個引數都要變化,可以用它
例:
mapply(rep, 1:4, 4:1)
[[1]]
[1] 1 1 1 1
[[2]]
[1] 2 2 2
[[3]]
[1] 3 3
[[4]]
[1] 4
4.outer()函式,類似矩陣計算中的外積,直接對兩個數相乘
outer(X, Y, FUN = "*", ...)
*函數可以自定義
例:
outer(month.abb, 1999:2003, FUN = "paste")
[,1] [,2] [,3] [,4] [,5]
[1,] "Jan 1999" "Jan 2000" "Jan 2001" "Jan 2002" "Jan 2003"
[2,] "Feb 1999" "Feb 2000" "Feb 2001" "Feb 2002" "Feb 2003"
[3,] "Mar 1999" "Mar 2000" "Mar 2001" "Mar 2002" "Mar 2003"
[4,] "Apr 1999" "Apr 2000" "Apr 2001" "Apr 2002" "Apr 2003"
[5,] "May 1999" "May 2000" "May 2001" "May 2002" "May 2003"
[6,] "Jun 1999" "Jun 2000" "Jun 2001" "Jun 2002" "Jun 2003"
[7,] "Jul 1999" "Jul 2000" "Jul 2001" "Jul 2002" "Jul 2003"
[8,] "Aug 1999" "Aug 2000" "Aug 2001" "Aug 2002" "Aug 2003"
[9,] "Sep 1999" "Sep 2000" "Sep 2001" "Sep 2002" "Sep 2003"
[10,] "Oct 1999" "Oct 2000" "Oct 2001" "Oct 2002" "Oct 2003"
[11,] "Nov 1999" "Nov 2000" "Nov 2001" "Nov 2002" "Nov 2003"
[12,] "Dec 1999" "Dec 2000" "Dec 2001" "Dec 2002" "Dec 2003"
#例如用其生成九九乘法表
f<-function(x,y){
left<-paste0(x,"*",y,"=")
right<-x*y
re<-paste0(left,right)
return(re)
}
vec1<-vec2<-vec
outer(vec1,vec2,FUN=f)
#結果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "1*1=1" "1*2=2" "1*3=3" "1*4=4" "1*5=5" "1*6=6" "1*7=7" "1*8=8" "1*9=9"
[2,] "2*1=2" "2*2=4" "2*3=6" "2*4=8" "2*5=10" "2*6=12" "2*7=14" "2*8=16" "2*9=18"
[3,] "3*1=3" "3*2=6" "3*3=9" "3*4=12" "3*5=15" "3*6=18" "3*7=21" "3*8=24" "3*9=27"
[4,] "4*1=4" "4*2=8" "4*3=12" "4*4=16" "4*5=20" "4*6=24" "4*7=28" "4*8=32" "4*9=36"
[5,] "5*1=5" "5*2=10" "5*3=15" "5*4=20" "5*5=25" "5*6=30" "5*7=35" "5*8=40" "5*9=45"
[6,] "6*1=6" "6*2=12" "6*3=18" "6*4=24" "6*5=30" "6*6=36" "6*7=42" "6*8=48" "6*9=54"
[7,] "7*1=7" "7*2=14" "7*3=21" "7*4=28" "7*5=35" "7*6=42" "7*7=49" "7*8=56" "7*9=63"
[8,] "8*1=8" "8*2=16" "8*3=24" "8*4=32" "8*5=40" "8*6=48" "8*7=56" "8*8=64" "8*9=72"
[9,] "9*1=9" "9*2=18" "9*3=27" "9*4=36" "9*5=45" "9*6=54" "9*7=63" "9*8=72" "9*9=81"
outer()在3d作圖中的應用。
f<-function(x,y,z) x^2-10*cos(2*pi*x)+y^2+10*cos(2*pi*y)+20
x<-y<-seq(-5,5,0.01)
z<-outer(x,y,f)
image(x,y,z)
Z<-rank(z)
library(rgl)
bg3d("slategray")#設定背景色
persp3d(x,y,z,col=rainbow(as.integer(max(Z)))[Z])
play3d(spin3d())