[R-SIG-Finance] Converting factors back to numbers in R

John C. Frain frainj at tcd.ie
Tue Dec 5 16:15:27 CET 2006


The html help file for factors gives

To “revert” a factor f to its original numeric values, as.numeric(levels(f))[f]
is recommended and slightly more efficient than as.numeric(as.character(f)).


John C. Frain.
Economics Department
Trinity College Dublin
Dublin 2

www.tcd.ie/Economics/staff/frainj/home.html
mailto:frainj at tcd.ie


Quoting ÒÏ×ÅÎ áËØÁÔÏÉ <volchik2000 at list.ru>:

> Hi everybody,
>
> i've spent a lot of time trying to fix the following problem:
>
> i'm trying to load into R several dataframes with stocks data through:
>
> dat<-lapply(1:Number.of.contracts,function(x) {
>    print(Names[x])
>    read.csv(paste(WorkingDirectory,Names[x],
>
'_',TimeFrame,'.csv',sep=''),as.is=T,header=T,row.names=1,na.strings=c("#NA",""))
>    })
>
>
> Because the header are times in the format "HH:MM" R converts them to
> strings, changing in the way. It's now a problem, i can fix it using sub
> (gsub) functions.
> Problem is that R converts prices to factors as well even though i have
> option as.is=T set, and then it almost impossible to convert them back to
> double preserving the row.names (these are dates) and column names (times),
> probably because of the structure i store them in (list of dataframes).
> I suspect there is an easy way to solve this problem, but couldn't figure it
> out :(
>
> Thanks
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>



More information about the R-SIG-Finance mailing list