0

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
VincentH
  • 1,009
  • 4
  • 13
  • 24
  • The column might not contain only numbers. But you did not even give us the error message ... Also, how exactly did you "build" the data.frame? – Roland Jun 28 '12 at 09:50
  • the error message must be something like NAs introduced while automatic conversion (my R is not in English...). As for the construction of the data frame: I imported that to a first data frame with an sql query Then I reorganised my data frame with cast/reshape – VincentH Jun 28 '12 at 09:55
  • I can also have (on subset of the data frame) object list cannot be converted to type 'double' – VincentH Jun 28 '12 at 09:58
  • Please provide your code (edit your question). Otherwise, it is not possible to help you. Also, looking at `levels(factor(df$asumed_to_be_numeric_vector))` might help you checking, if values are all numeric. Non-numeric values would be at the end of the output. – Roland Jun 28 '12 at 10:56
  • @Roland: levels returned character(0). I tested the mode of a few random elements. The mode is "numeric" – VincentH Jun 28 '12 at 11:56
  • You can step over the function by inserting `browser` at the beginning (or where break desired) and run the function in question. Can you `dput` the head of your data? – Roman Luštrik Jun 28 '12 at 12:20
  • 4
    I think you have bigger problems than conversion from character to numeric. The function you posted is a mess of spaghetti code that will be very tricky to debug. Try to rewrite it so that you do one thing at a time, and try to remove those `for` loops. – Richie Cotton Jun 28 '12 at 12:25
  • @RomanLuštrik: Thanks for browser did not know about that. I never used dput. I see in the help it enables to deparse. I can dput the header of my data frame. – VincentH Jun 28 '12 at 12:40
  • @RichieCotton: You are right. I am probably better off rewriting this function. – VincentH Jun 28 '12 at 12:41
  • Or at least rewrite it in its current form so that you break up the lines that start with `for(...` into one step per line. That is, where there is a semi-colon, start a new line. That way `browser` will be easier. – BenBarnes Jun 28 '12 at 13:47

0 Answers0