Re: [R] R- transform data frame into matrix

From: Dieter Menne <>
Date: Wed, 12 Nov 2008 17:01:20 +0000 (UTC)

Thorsten Raff <t.raff <at>> writes:

> I have the problem that I want to transform a dataframe as generated by
> diagnosis <- rep(diagnosis[1:3], 3)
> marker <- gl(3,3)
> values <- rnorm(9)
> dataframe <- cbind(diagnosis, marker, values)
> dataframe <- dataframe[c(1:5, 7:9), ]
> into a matrix where levels(diagnosis) is indicating the rows of the matrix,
> levels(marker) the columns and values are the actual content of the matrix.
> However, as can been seen in the dataframe, some observations are missing and
> should result in <NAs> in the resulting matrix, giving a result like this:
> marker 1 2 3
> diagnosis1 value value value
> diagnosis2 value value value
> diagnosis3 value NA value

You can make your examples self-running by providing fake data for diagnosis, e.g. using letters[] instead of diagnosis. In addition, note that your cbind has an awkward side effect by converting everything to character as the least common denominator for the variables in a matrix.

For real work, use reshape with its frightening number of parameters. If you don't understand that function at the seventh reading, stay cool: it's not your fault. You might also try package reshape, but it is not much easier for occasional use.


diagnosis <- as.factor(rep(letters[1:3], 3)) marker <- as.factor(gl(3,3))
values <- rnorm(9)
dataframe <- data.frame(diagnosis=diagnosis, marker=marker, values=values) dataframe <- dataframe[c(1:5, 7:9), ]

reshape(dataframe,idvar="diagnosis",direction="wide",timevar="marker") mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 12 Nov 2008 - 17:04:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 12 Nov 2008 - 17:30:26 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive