R語言數據重塑sapply、ddply

r語言相關知識

BMAverageDrawDown<-function(Rp,…){
#tmp[tmp<0],即爲回撤序列
tmp <- period.apply(Rp,PTTurningPoint(Rp),FUN=function(Rp){min(cumprod(1+Rp)-1)})
-mean(tmp[tmp<0])
}
計算平均回撤率,輸入一個xts格式的序列,產生一個值。
period.apply(x, INDEX, FUN, …)將一段序列x根據index分段用函數FUN作用於每一段x。index爲每個區間兩端的節點。最後得到的序列長度爲index-1.

odf <- xts::xts(odf[,c(「RP」,「RM」)],order.by = as.Date(as.character(odf S K D A T E ) , f o r m a t = &quot; d a t a . f r a m e x t s x t s : : x t s ( ) l i b r a r y ( x t s ) ; x t s ( ) . a s . D a t e 20170620 2017 06 20 20170620 o d f SK_DATE),format=&quot;%Y%m%d&quot;)) 將原來的data.frame數據轉化爲xts格式,xts::xts()相當於library(xts);xts(). as.Date可以將「20170620」轉化爲「2017-06-20」的時間格式,注意20170620必須是字符形式。此時用odf Rp與轉化前odf$Rp是不同的,前者是xts格式,後者不是。

vlist <- ls(envir=.GlobalEnv)
vlist <- sort(vlist[substr(vlist,1,2)==「BM」])
第一個:調用環境中所有出現的變量的名字,包括函數的名字
第二個:substr(vlist,1,2)將vlist字符中第一第二個字符

sapply(vlist,wrapper2,args=list(Rp=Rp,Rm=Rm,Rf=Rf,MAR=MAR,p=p,GEO=GEO))
將wrapper2作用於vlist中所有的元素
sapply是使wrapper2作用於vlist上,arg爲wrapper要用到的參數。其中vlist還是一組字符串格式。因爲wrapper要求輸入參數爲函數名。

a <- try(do.call(p_func_name,args=…),silent=TRUE)
do.call(a,args=list(c1,c2,c3))就相當於a(c1),a是函數c1是參數。
簡單的時候用a(c1),但是當換了個函數b,他的輸入參數爲c2時那就很有用了。因爲arg不用變,只需要a改爲b就可以。
try可以判斷將出現錯誤的程序繼續運行,只是將錯誤報告賦值給另一個變量,注意的是silent=TRUE必須加上就是「保持沉默」,忽略錯誤,繼續運行。

INDICATORS=gsub(「BM」,"",vlist)
將vlist中的BM字符刪去
gsub (pattern, replacement, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE),將x中的pattern部分替換爲replacement

start_time <- as.numeric(format(today()%m-%months(3),"%Y%m%d"))
用lubridate包來處理時間數據,today()是其中的一個函數表示去今天的日期比如會返回」20170620「.
%m-%相當於減號,可以進行時間上的減法,也可以用「-」,但是5月31日減去一個月是4月3
0日,用「-」會返回NA,用%m-%返回的是4月30日。

re <- sapply(as.array(re),"[",1)
re中每個分量只取第一個數。防止某個指標結果有兩個一樣的。

re_r3m <- ddply(df[df S K D A T E &gt; = s t a r t t i m e , ] ,   B K P R O D U C T , c a l c I n d i c a t o r s 2 ) d d p l y ( m y d a t a , c ( c o l u m n n a m e o f a f a c t o r t o g r o u p b y , c o l u m n n a m e o f t h e s e c o n d f a c t o r t o g r o u p b y ) , s u m m a r i z e O R t r a n s f o r m , n e w c o l u m n = m y f u n c t i o n ( c o l u m n n a m e ( s ) I w a n t t h e f u n c t i o n t o a c t u p o n ) ) d d p l y ( ) c o l u m n d d p l y ( ) c o l u m n f a c t o r c o l u m n d d p l y ( ) 使 . ( c o l u m n , c o l u m n ) , 使   c o l u m n , c o l u m n N U L L w i t h ( a , o r d e r ( c , d ) ) o r d e r ( a SK_DATE&gt;=start_time,],~BK_PRODUCT,calcIndicators2) 分組對數據進行函數操作 ddply(mydata, c(&#x27;column name of a factor to group by&#x27;, &#x27;column name of the second factor to group by&#x27;), summarize OR transform, newcolumn = myfunction(column name(s) I want the function to act upon)) 函數ddply()的第一個參數是原始數據框名稱,第二個參數則是我們打算劃分爲子集的column名稱。第三個參數用於告知ddply()應該返回數據點結果(即總和)還是在整套數據框的新column中根據factor的具體要求顯示每行數據點內容。最後,第四項參數用於命名新column並列出我們希望ddply()使用的函數清單。 其中第二個參數可以替換爲 .(column,column),如果爲一列的話可以使用~column,如我們的原句所示或者「column」,當然其值也可以爲NULL這樣就可以不分組,對全局運行函數。 with(a,order(-c,d))相當於order(-a c,a$d),其中order(-c)表示對c從大到小排序。
10.
數據重塑reshape2,melt,dcast
companiesLong <- melt(companiesData, id.vars=c(「fy」, 「company」),
measure.vars=c(「revenue」, 「profit」, 「margin」),
variable.name=「financialCategory」, value.name=「amount」)
數據由
在這裏插入圖片描述
變爲
在這裏插入圖片描述 companiesWide <- dcast(companiesLong, fy + company ~ financialCategory, value.var=「amount」) 此程序數據就又會變回去。