Skip to content

Help on loops

3 messages · Anup Nandialath, jim holtman

#
use split and lapply to make it easier.  Substitute in your own
calculations since this is just an example:
id t X1 X2
1  1 1  4  3
2  1 2  9  2
3  1 3  7  3
4  1 4  6  6
5  2 1  6  4
6  2 2  5  3
7  2 3  1  1
8  3 1  9  6
9  3 2  5  5
+     output <- .grp[1, 3:4] * 4  # data from the first row
+     i <- 2
+     # repeat for the rest of the row
+     while (i <= nrow(.grp)){
+         output <- output + .grp[i, 3:4] * 2
+         i <- i + 1
+     }
+     output  # return value
+ })
$`1`
  X1 X2
1 60 34

$`2`
  X1 X2
5 36 24

$`3`
  X1 X2
8 46 34
On Tue, Dec 7, 2010 at 5:43 PM, Anup Nandialath
<anup_nandialath at yahoo.com> wrote: