Re: [R] Simple loop

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Tue, 03 May 2011 12:37:23 -0700

Hi:

Here are two more candidates, using packages plyr and data.table. Your toy data frame is called dd below.

library(plyr)
ddply(dd, .(Site, Prof), transform, Hadj = H - min(H))   Site Prof H Hadj
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

library(data.table)
dt <- data.table(dd, key = 'Site, Prof') dt[, list(H = H, Hadj = H - min(H)), by = 'Site, Prof']

<same output as above>

HTH,
Dennis

On Tue, May 3, 2011 at 8:44 AM, Woida71 <w.gostner_at_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_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.
>



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. Received on Thu 05 May 2011 - 06:25:04 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 Thu 05 May 2011 - 07:00:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive