[R] How to get minimum value by group

2010-01-11 Thread JustinNabble

I'd like to get a long data set of minimum values from groups in another data
set.
The following almost does what I want. (Note, I'm using the word factor
differently from it's meaning in R; bad choice of words)

myframe = data.frame(factor1 = rep(1:2,each=8), factor2 =
rep(c(a,b),each=4, times=2), factor3 = rep(c(x,y),each=2, times=4),
y=1:16)
attach(myframe)
minimums = by(y, list(factor1, factor2,factor3), min)
detach(myframe)

The problem is that minimums is object of class by, which looks like
some kind of array with the number of dimension equal to the number of
factors. I just want two dimensions though, with factors in columns. I can't
figure out how to reformat it to something like this:

factor1 factor2 factor2 y
1 a x 1
2 a x 9
1 b x 5
...

I could make nested for loops, but I'd like something that will work for an
arbitrary number of factors. I've seen some functions that will rearrange
data (e.g. reshape), but they all seem to require a data.frame to start
with.
-- 
View this message in context: 
http://n4.nabble.com/How-to-get-minimum-value-by-group-tp1011745p1011745.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] How to get minimum value by group

2010-01-11 Thread David Winsemius


On Jan 11, 2010, at 7:58 PM, JustinNabble wrote:



I'd like to get a long data set of minimum values from groups in  
another data

set.
The following almost does what I want. (Note, I'm using the word  
factor

differently from it's meaning in R; bad choice of words)

myframe = data.frame(factor1 = rep(1:2,each=8), factor2 =
rep(c(a,b),each=4, times=2), factor3 = rep(c(x,y),each=2,  
times=4),

y=1:16)
attach(myframe)


# with(myframe,   ) would be a better construction

minimums = by(y, list(factor1, factor2,factor3), min)
detach(myframe)


It's a table, which IS like an array, You want:

 as.data.frame.table(minimums)
  Var1 Var2 Var3 Freq
11ax1
22ax9
31bx5
42bx   13
51ay3
62ay   11
71by7
82by   15



The problem is that minimums is object of class by, which looks  
like

some kind of array with the number of dimension equal to the number of
factors. I just want two dimensions though, with factors in columns.  
I can't

figure out how to reformat it to something like this:

factor1 factor2 factor2 y
1 a x 1
2 a x 9
1 b x 5
...

I could make nested for loops, but I'd like something that will work  
for an
arbitrary number of factors. I've seen some functions that will  
rearrange
data (e.g. reshape), but they all seem to require a data.frame to  
start

with.
--

--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
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.


Re: [R] How to get minimum value by group

2010-01-11 Thread Peter Alspach
Tena koe Justin

Try aggregate(): e.g., 

aggregate(myframe[,4], myframe[,1:3], min)

HTH 

Peter Alspach

 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of JustinNabble
 Sent: Tuesday, 12 January 2010 1:58 p.m.
 To: r-help@r-project.org
 Subject: [R] How to get minimum value by group
 
 
 I'd like to get a long data set of minimum values from groups 
 in another data set.
 The following almost does what I want. (Note, I'm using the 
 word factor differently from it's meaning in R; bad choice of words)
 
 myframe = data.frame(factor1 = rep(1:2,each=8), factor2 = 
 rep(c(a,b),each=4, times=2), factor3 = 
 rep(c(x,y),each=2, times=4),
 y=1:16)
 attach(myframe)
 minimums = by(y, list(factor1, factor2,factor3), min)
 detach(myframe)
 
 The problem is that minimums is object of class by, which 
 looks like some kind of array with the number of dimension 
 equal to the number of factors. I just want two dimensions 
 though, with factors in columns. I can't figure out how to 
 reformat it to something like this:
 
 factor1 factor2 factor2 y
 1 a x 1
 2 a x 9
 1 b x 5
 ...
 
 I could make nested for loops, but I'd like something that 
 will work for an arbitrary number of factors. I've seen some 
 functions that will rearrange data (e.g. reshape), but they 
 all seem to require a data.frame to start with.
 --
 View this message in context: 
 http://n4.nabble.com/How-to-get-minimum-value-by-group-tp10117
 45p1011745.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 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.
 

__
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.


Re: [R] How to get minimum value by group

2010-01-11 Thread Gabor Grothendieck
Try this:

 aggregate(myframe[4], myframe[1:3], min)
  factor1 factor2 factor3  y
1   1   a   x  1
2   2   a   x  9
3   1   b   x  5
4   2   b   x 13
5   1   a   y  3
6   2   a   y 11
7   1   b   y  7
8   2   b   y 15


On Mon, Jan 11, 2010 at 7:58 PM, JustinNabble
justinmmcgr...@hotmail.com wrote:

 I'd like to get a long data set of minimum values from groups in another data
 set.
 The following almost does what I want. (Note, I'm using the word factor
 differently from it's meaning in R; bad choice of words)

 myframe = data.frame(factor1 = rep(1:2,each=8), factor2 =
 rep(c(a,b),each=4, times=2), factor3 = rep(c(x,y),each=2, times=4),
 y=1:16)
 attach(myframe)
 minimums = by(y, list(factor1, factor2,factor3), min)
 detach(myframe)

 The problem is that minimums is object of class by, which looks like
 some kind of array with the number of dimension equal to the number of
 factors. I just want two dimensions though, with factors in columns. I can't
 figure out how to reformat it to something like this:

 factor1 factor2 factor2 y
 1         a         x         1
 2         a         x         9
 1         b         x         5
 ...

 I could make nested for loops, but I'd like something that will work for an
 arbitrary number of factors. I've seen some functions that will rearrange
 data (e.g. reshape), but they all seem to require a data.frame to start
 with.
 --
 View this message in context: 
 http://n4.nabble.com/How-to-get-minimum-value-by-group-tp1011745p1011745.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 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.


__
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.