I have a data frame that I build with data from an sql db. First column contains date, the other contain double or string which should be double.
I need to convert the string to real double.
I have tried to extract the data with "numbers" in another data frame and then: as.numeric or class = numeric.
Both those options return error.
Any idea?
Many thanks, Vincent
edit: here is the function where I got my issue/
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)
}
My TablePrice look (in bigger) like:
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