0

我有一个使用 sql 数据库中的数据构建的数据框。第一列包含日期,另一列包含双精度或字符串,应为双精度。

我需要将字符串转换为真正的双精度。

我试图在另一个数据框中提取带有“数字”的数据,然后:as.numeric 或 class = numeric。

这两个选项都返回错误。

任何想法?

非常感谢,文森特

编辑:这是我遇到问题的功能/

 function(Best,Worst,PriceTable){
PnL=data.frame()

#PnL long
for(i in 1:(nrow(Best)-1)){PnL[i,1]=Best[i+1,1];pnl=0;divide=0;for (j in 1:ncol(Best)){if((!is.null(PriceTable[i+1,as.character(Best[i,j])])) && (!is.na(as.numeric(PriceTable[i+1,as.character(Best[i,j])])))){pnl=pnl+as.numeric(PriceTable[i+1,as.character(Best[i,j])]);divide=divide+1}};PnL[i,2]=pnl/divide;PnL[i,3]=0;PnL[i,4]=divide}

#PnL short
for(i in 1:(nrow(Worst)-1)){pnl=0;divide=0;for (j in 1:ncol(Worst)){if((!is.null(PriceTable[i+1,as.character(Worst[i,j])])) && (!is.na(as.numeric(PriceTable[i+1,as.character(Worst[i,j])])))){pnl=pnl+as.numeric(PriceTable[i+1,as.character(Worst[i,j])]);divide=divide+1}};PnL[i,3]=-pnl/divide;PnL[i,5]=divide}

colnames(PnL)[1]='Date'
colnames(PnL)[2]='PnL Long'
colnames(PnL)[3]='PnL Short'
colnames(PnL)[4]='N trade long'
colnames(PnL)[5]='N trade short'

#overall PnL
for (i in 1:nrow(PnL)){PnL[i,6]=PnL[i,3]+PnL[i,2]}
colnames(PnL)[6]='PnL'

return(PnL)

}

我的 TablePrice 看起来(更大)如下:

row.names   Date        25179M103   371532102   186905105   382388106
16         2011-01-25   0.08094573   0.001366867    -0.08003574 0.02431649
21         2011-02-01   0.10623324   0.033948941     0.02128497 0.02949376
40         2011-03-01   0.02046414  -0.028119508    -0.16465137 -0.09292013
63         2011-04-01   0.04486064   0.034553366    0.01336088  0.02603872
83         2011-05-02  -0.02220955  -0.016687422    -0.07369759 0.03656137
4

0 回答 0