Skip to content
Prev 136596 / 398498 Next

Creating a data.frame

On 13-Feb-08 22:17:32, Joe Trubisz wrote:
You don't need to force it! It's not obvious what made you
think of using cbind() internally, but it doesn't do what
you intended. Simply:

  d<-data.frame(x=I(x),y=y,z=z)
  d
#     x y z
# 1 aaa 0 0
# 2 bbb 0 0
# 3 ccc 0 0
  str(d)
# 'data.frame':   3 obs. of  3 variables:
#  $ x:Class 'AsIs'  chr [1:3] "aaa" "bbb" "ccc"
#  $ y: num  0 0 0
#  $ z: num  0 0 0

The trouble was that cbind(x=I(x),y=y,z=z) makes a matrix,
and you cannot mix types in a matrix, so this will coerce
all the variables to character type. So it's your original
way which does the forcing!

Hoping this helps,
Ted.



--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 13-Feb-08                                       Time: 23:24:30
------------------------------ XFMail ------------------------------