Skip to content

Help with indexing

3 messages · Dana Sevak, William Dunlap, jim holtman

#
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
#
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
match() will do it:
   > match(names, unique(names)) + 99
   [1] 100 100 100 101 101 102 102 102 102

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
try this:
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
On Sat, Nov 21, 2009 at 7:00 PM, Dana Sevak <dana.sevak at yahoo.com> wrote: