[R] How to avoid conversion to factors (data frame to zoo)

Li, Yan (IED) Yan.Y.Li at MorganStanley.com
Thu Oct 18 16:38:30 CEST 2007


Hi all,

I was trying to convert a data frame to a zoo object so I can use some
time series functions like lag(). But it seems then everything became a
factor, so I have to convert it back to numeric to run the correct
regressions. Is there a way to avoid it? Here is an example:

#############################
a <- data.frame(nn =as.character(c("a", "b", "c", "d")), dd =
as.Date("2007-08-01")+ c(1,2,3,4), x = rnorm(4), y = rnorm(4))
a
str(a)


b <- zoo(a, order.by = a$dd)
b
str(b)

c <- data.frame(b)
str(c)

#############################################333
Results of this example:
> a
  nn         dd      x      y
1  a 2007-08-02  0.388 -0.394
2  b 2007-08-03 -0.054 -0.059
3  c 2007-08-04 -1.377  1.100
4  d 2007-08-05 -0.415  0.763
> str(a)
'data.frame':   4 obs. of  4 variables:
 $ nn: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
 $ dd:Class 'Date'  num [1:4] 13727 13728 13729 13730
 $ x : num   0.3877 -0.0538 -1.3771 -0.4150
 $ y : num  -0.3943 -0.0593  1.1000  0.7632
> b <- zoo(a, order.by = a$dd)
> b
           nn dd         x      y     
2007-08-02 a  2007-08-02  0.388 -0.394
2007-08-03 b  2007-08-03 -0.054 -0.059
2007-08-04 c  2007-08-04 -1.377  1.100
2007-08-05 d  2007-08-05 -0.415  0.763
> str(b)
 chr [1:4, 1:4] "a" "b" "c" "d" "2007-08-02" "2007-08-03" ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:4] "1" "2" "3" "4"
  ..$ : chr [1:4] "nn" "dd" "x" "y"
 - attr(*, "index")=Class 'Date'  num [1:4] 13727 13728 13729 13730
> 
> c <- data.frame(b)
> str(c)
'data.frame':   4 obs. of  4 variables:
 $ nn: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
  ..- attr(*, "names")= chr  "1" "2" "3" "4"
 $ dd: Factor w/ 4 levels "2007-08-02","2007-08-03",..: 1 2 3 4
  ..- attr(*, "names")= chr  "1" "2" "3" "4"
 $ x : Factor w/ 4 levels " 0.388","-0.054",..: 1 2 4 3
  ..- attr(*, "names")= chr  "1" "2" "3" "4"
 $ y : Factor w/ 4 levels " 0.763"," 1.100",..: 4 3 2 1
  ..- attr(*, "names")= chr  "1" "2" "3" "4"
##################################3
So after converting to zoo, all the variables became factors. How can I
keep this from happening? Thank you very much for any advice.

Yan
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to bu...{{dropped:22}}



More information about the R-help mailing list