[R] Help with indexing

Dana Sevak dana.sevak at yahoo.com
Sun Nov 22 01:00:27 CET 2009


Dear R Helpers,

I am missing something very elementary here, and I don't seem to get it from the help pages of the ave, seq and seq_along functions, so I wonder if you could offer a quick help.

To use an example from an earlier post on this list, I have a dataframe of this kind:

dat = data.frame(name = rep(c("Mary", "Sam", "John"), c(3,2,4))) 
dat$freq = ave(seq_along(dat$name), dat$name, FUN = seq_along)

dat 
  name freq
1 Mary    1
2 Mary    2
3 Mary    3
4  Sam    1
5  Sam    2
6 John    1
7 John    2
8 John    3
9 John    4

What I need is another column assigning a number to each name starting from index 100, that is:

  name freq  nb
1 Mary    1 100
2 Mary    2 100
3 Mary    3 100
4  Sam    1 101
5  Sam    2 101
6 John    1 102
7 John    2 102
8 John    3 102
9 John    4 102

What is the easiest way to do this?

Thanks a lot for your kind help.

Dana




More information about the R-help mailing list