Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of andrija djurovic
> Sent: Tuesday, May 03, 2011 11:28 AM
> To: Woida71
> Cc: r-help@r-project.org
> Subject: Re: [R] Simple loop
> 
> Hi.
> There is no need to do this in a for loop.
> Here is one approach:
> 
> x <- read.table(textConnection("Site  Prof  H
> 1      1     24
> 1      1     16
> 1      1     67
> 1      2     23
> 1      2     56
> 1      2     45
> 2      1     67
> 2      1     46"), header = TRUE)
> closeAllConnections()
> x
> cbind(x,newCol=unlist(tapply(x[,3],paste(x[,1],x[,2],sep=""),
> function(x) x-min(x)))
>    Site Prof  H newCol
> 111    1    1 24      8
> 112    1    1 16      0
> 113    1    1 67     51
> 121    1    2 23      0
> 122    1    2 56     33
> 123    1    2 45     22
> 211    2    1 67     21
> 212    2    1 46      0

That works when Site and Prof are ordered as shown, but if 
they are not sorted cbind(...,tapply) won't line up the the
new entries with the old rows properly.  Try doing it on
x[8:1,] to see this.  

ave() can deal that problem:
  > cbind(x, newCol2 = with(x, ave(H, Site, Prof,
FUN=function(y)y-min(y))))
    Site Prof  H newCol2
  1    1    1 24       8
  2    1    1 16       0
  3    1    1 67      51
  4    1    2 23       0
  5    1    2 56      33
  6    1    2 45      22
  7    2    1 67      21
  8    2    1 46       0
  Warning message:
  In min(y) : no non-missing arguments to min; returning Inf
The warning is unfortunate: ave() calls FUN even for when
there is no data for a particular group (Site=2, Prof=2 in this
case).

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 


> 
> Andrija
> 
> 
> On Tue, May 3, 2011 at 5:44 PM, Woida71 <w.gost...@ipp.bz.it> wrote:
> 
> > Hello everybody,
> > I am beginning with loops and functions and would be glad 
> to have help in
> > the following question:
> > If i have a dataframe like this
> > Site  Prof  H
> > 1      1     24
> > 1      1     16
> > 1      1     67
> > 1      2     23
> > 1      2     56
> > 1      2     45
> > 2      1     67
> > 2      1     46
> > And I would like to create a new column that subtracts the 
> minimum of H
> > from
> > H, but for S1 and P1
> > only the minimum of the data points falling into this 
> category should be
> > taken.
> > So for example the three first numbers of the new column 
> write: 24-16,
> > 16-16, 67-16
> > the following numbers refering to Site1 and Prof2 write: 
> 23-23, 56-23,
> > 45-23.
> > I think with two loops one refering to the Site, the other 
> to the Prof, it
> > should be possible to automatically
> > create the new column.
> > Thanks a lot for any help.
> >
> > --
> > View this message in context:
> > http://r.789695.n4.nabble.com/Simple-loop-tp3492819p3492819.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.
> >
> 
>       [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

Reply via email to