Hello, I have a dataframe with columns like: parameter1 parameter2 metricname value and I'd like to transform it into a dataframe with columns: parameter1 parameter2 metric1 metric2 metric3 where the values for each metric would be in the appropriate column. There are often multiple values for a given (parameter1, parameter2, metricname) triple. In this case, I want to use the minimum value. A constant number of metrics is associated with each possible combination of parameters. I've cobbled together code that performs this using nested loops and far too many variables. The number of metrics has now gone up by an order of magnitude and I'm looking for a more elegant way of doing this. If anyone has a short bit of code that can deal with this, I'd really appreciate having a look. Thanks much, Barry
Dear Barry, Have a look at the function cast() in the reshape package. That should be able to do what you need. HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Barry Rountree Verzonden: donderdag 20 november 2008 8:39 Aan: r-help op r-project.org Onderwerp: [R] Elegant way to transform dataframe? Hello, I have a dataframe with columns like: parameter1 parameter2 metricname value and I'd like to transform it into a dataframe with columns: parameter1 parameter2 metric1 metric2 metric3 where the values for each metric would be in the appropriate column. There are often multiple values for a given (parameter1, parameter2, metricname) triple. In this case, I want to use the minimum value. A constant number of metrics is associated with each possible combination of parameters. I've cobbled together code that performs this using nested loops and far too many variables. The number of metrics has now gone up by an order of magnitude and I'm looking for a more elegant way of doing this. If anyone has a short bit of code that can deal with this, I'd really appreciate having a look. Thanks much, Barry ______________________________________________ R-help op 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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
---- Original message ---->Date: Thu, 20 Nov 2008 09:41:11 +0100 >From: "David Hajage" <dhajage.r at gmail.com> >Subject: Re: [R] Elegant way to transform dataframe? >To: "Barry Rountree" <rountree at uga.edu> > > I suggest you to have a look to the reshape package, > by Hadley Wickham : http://had.co.nz/reshape/ >That's exactly what I needed -- thanks! A few comments for other people who might be interested in trying it: 1) "reshape" is the name of the package, "melt" and "cast" are the commands. There's a "reshape" command that's part of the stats package and it does something similar, but it's not nearly so general. 2) I didn't find the package documentation helpful, but the website has links to several papers that provide much better examples. 3) The package requires an up-to-date version of R. I had been using an old version and trying to install the package gave a "not found" error. It wasn't until I downloaded the package and tried a local install that I got a useful error message about the version mismatch. Thanks again, Barry> 2008/11/20 Barry Rountree <rountree at uga.edu> > > Hello, > > I have a dataframe with columns like: > > parameter1 parameter2 metricname value > > and I'd like to transform it into a dataframe with > columns: > > parameter1 parameter2 metric1 metric2 metric3 > > where the values for each metric would be in the > appropriate column. There are often multiple > values for a given (parameter1, parameter2, > metricname) triple. In this case, I want to use > the minimum value. > > A constant number of metrics is associated with > each possible combination of parameters. > > I've cobbled together code that performs this > using nested loops and far too many variables. > The number of metrics has now gone up by an order > of magnitude and I'm looking for a more elegant > way of doing this. > > If anyone has a short bit of code that can deal > with this, I'd really appreciate having a look. > > Thanks much, > > Barry > > ______________________________________________ > R-help at 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.