Hello 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 Can anyone help out with some code for this? I have tried to look this up in the mailing list as it has probably been answered before but only found aspects of the problem which I was just not able to plumb together. Many thanks in advance, I hope this is not to much of an insult for the list. Thorsten P.S: I have already sent this message to the mailing list, howver I got a bounce-message back, so hopefully this is no duplicate entry. Sorry for any inconvenience that this may confer. -- Thorsten Raff 2nd Medical Department, University Hospital Schleswig-Holstein, Campus Kiel Chemnitzstra?e 33 24116 Kiel GERMANY phone: +49 431 1697-5234 fax: +49 431 1697-1264 email: t.raff<at>med2.uni-kiel.de web: www.uk-s-h.de -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: This is a digitally signed message part. URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081112/6dee27fd/attachment.bin>
Thorsten Raff <t.raff <at> med2.uni-kiel.de> 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. Dieter # 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")
Try this also: xtabs(values ~ diagnosis + marker, data=dataframe) On Wed, Nov 12, 2008 at 2:31 PM, Thorsten Raff <t.raff@med2.uni-kiel.de>wrote:> Hello > > 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 > > Can anyone help out with some code for this? I have tried to look this up > in > the mailing list as it has probably been answered before but only found > aspects of the problem which I was just not able to plumb together. > > Many thanks in advance, I hope this is not to much of an insult for the > list. > > Thorsten > > P.S: I have already sent this message to the mailing list, howver I got a > bounce-message back, so hopefully this is no duplicate entry. Sorry for any > inconvenience that this may confer. > > > -- > Thorsten Raff > 2nd Medical Department, > University Hospital Schleswig-Holstein, Campus Kiel > Chemnitzstraße 33 > 24116 Kiel > GERMANY > > phone: +49 431 1697-5234 > fax: +49 431 1697-1264 > > email: t.raff<at>med2.uni-kiel.de > web: www.uk-s-h.de > > > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Thank you very much, looks rather simple when viewed from behind :(> Try this also: > xtabs(values ~ diagnosis + marker, data=dataframe)> 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.> Dieter> 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")>> 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 >>-- Thorsten Raff 2nd Medical Department, University Hospital Schleswig-Holstein, Campus Kiel Chemnitzstra?e 33 24116 Kiel GERMANY phone: +49 431 1697-5234 fax: +49 431 1697-1264 email: t.raff<at>med2.uni-kiel.de web: www.uk-s-h.de -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: This is a digitally signed message part. URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081112/99359e33/attachment.bin>