Skip to content

transforming a data frame to matrix

4 messages · Adel ESSAFI, John Sorkin, David Winsemius

#
Adel,

I don't fully understand your question. Please try to explain what you
want to do again.
In general to change a data frame to a matrix, all one needs to do is
use the as.matrix function.

# Create dataframe
mydataframe1 <- data.frame(x=1:3, y=10:12)
mydataframe1
class(mydataframe1)

# Convert to matrix
mymatrix1 <- as.matrix(mydataframe1)
mymatrix1
class(mymatrix1)

You must remember that data frames can contain values expressed as
numbers or characters. A matrix on the other hand normally contains only
numbers. Thus trying to convert a dataframe, such as yours to a matrix
will result in something that you probably don't want. Note the elements
in the matrix are all character values, and you probably want numeric
values:

# Create dataframe
mydataframe2 <- data.frame(x=1:3, y=10:12, letters = c("a","b","c"))
mydataframe2
class(mydataframe2)

# Convert to matrix
mymatrix2 <- as.matrix(mydataframe2)
mymatrix2
class(mymatrix2)

John










John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
Hello
Group.1 Group.2 Group.3  x    V5
7        C       L     0.0 30 C / L
19       C       L     0.2 27 C / L
31       C       L     0.4 15 C / L
43       C       L     0.6  7 C / L
54       C       L     0.8  2 C / L
10       C       S     0.0 27 C / S
22       C       S     0.2 10 C / S
34       C       S     0.4  6 C / S
46       C       S     0.6  1 C / S
1        D       D     0.0 30 D / D
13       D       D     0.2 30 D / D
25       D       D     0.4 17 D / D
37       D       D     0.6  9 D / D
49       D       D     0.8  2 D / D
3        D       F     0.0 30 D / F
15       D       F     0.2 26 D / F
27       D       F     0.4 13 D / F
39       D       F     0.6  3 D / F
51       D       F     0.8  3 D / F
5        D       I     0.0 26 D / I
17       D       I     0.2  7 D / I
29       D       I     0.4  7 D / I
41       D       I     0.6  1 D / I
53       D       I     0.8  1 D / I
8        D       L     0.0 30 D / L
20       D       L     0.2 26 D / L
32       D       L     0.4 14 D / L
44       D       L     0.6  8 D / L
55       D       L     0.8  2 D / L
11       D       S     0.0 27 D / S
23       D       S     0.2  8 D / S
35       D       S     0.4  4 D / S
47       D       S     0.6  1 D / S
2        I       D     0.0 30 I / D
14       I       D     0.2 30 I / D
26       I       D     0.4 14 I / D
38       I       D     0.6  9 I / D
50       I       D     0.8  2 I / D
4        I       F     0.0 30 I / F
16       I       F     0.2 29 I / F
28       I       F     0.4 16 I / F
40       I       F     0.6  5 I / F
52       I       F     0.8  2 I / F
6        I       I     0.0 30 I / I
18       I       I     0.2 17 I / I
30       I       I     0.4  4 I / I
42       I       I     0.6  1 I / I
9        I       L     0.0 30 I / L
21       I       L     0.2 27 I / L
33       I       L     0.4 17 I / L
45       I       L     0.6  8 I / L
56       I       L     0.8  2 I / L
12       I       S     0.0 28 I / S
24       I       S     0.2 11 I / S
36       I       S     0.4  6 I / S
48       I       S     0.6  3 I / S

I have a data frame formed by 5 colomns. How to transform this data
frame
to a matrix formed as follow:
1. the colomn 5 is the comumn (string)
2.  the colomn 5 row
3. the data are the colomn x.

I aim to draw barplot later using the matrix.

Thanks for any help
#
On Feb 18, 2012, at 8:35 AM, Adel ESSAFI wrote:

            
Perhaps:

xtabs(C ~ A + B, data=dfrm)

There will be an implicit summation of C-values in the presence of  
duplicates. For the application of other functions, you would use  
tapply. This untested code might give you counts:

with(dfrm, tapply(C, list(A, B), length)