[R] transforming a data frame to matrix

John Sorkin JSorkin at grecc.umaryland.edu
Sat Feb 18 14:54:23 CET 2012


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)

>>> Adel ESSAFI <adelessafi at gmail.com> 2/18/2012 7:57 AM >>>
Hello

> orderulcount
   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









-- 
PhD candidate in Computer Science
Address
3 avenue lamine, cité ezzahra, Sousse 4000
Tunisia
tel: +216 97 246 706 (+33640302046 jusqu'au 15/6)
fax: +216 71 391 166

	[[alternative HTML version deleted]]


Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}



More information about the R-help mailing list