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”) 此程序数据就又会变回去。