s-news
[Top] [All Lists]

Re: extracting a matrix from a data frame

To: <chris1@psyctc.org>
Subject: Re: extracting a matrix from a data frame
From: Tim Hesterberg x319 <timh@insightful.com>
Date: Sun, 25 Mar 2001 14:47:09 -0800
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <3ABCF4AC.23714.C2BB049@localhost> (chris1@psyctc.org)
References: <3ABCF4AC.23714.C2BB049@localhost>
Reply-to: timh@insightful.com (Tim Hesterberg)
The question was how to convert some columns in a data frame into
an ordinary matrix.

Use e.g.
        numerical.matrix(df[,3:6])
        numerical.matrix(df[3:6])  # faster, but differs for data.frame & matrix
        as.matrix(df[3:6])
        data.matrix(df[3:6])

The three functions differ in how they handle non-numerical
variables like factors.  Here are examples:

temp _ data.frame(a=1:2, b=letters[1:2], c=c(T,F))
S4> numerical.matrix(temp)
  a  b c 
1 1 NA 1
2 2 NA 0
S4> numerical.matrix(temp, remove=T)
  a c 
1 1 1
2 2 0
S4> as.matrix(temp)
    a   b   c 
1 "1" "a" "T"
2 "2" "b" "F"
S4> data.matrix(temp)
  a b c 
1 1 1 1
2 2 2 0
attr(, "column.levels"):
$a:
NULL

$b:
[1] "a" "b"

$c:
NULL


Tim Hesterberg

<Prev in Thread] Current Thread [Next in Thread>