[R] Help with indexing
jim holtman
jholtman at gmail.com
Sun Nov 22 01:45:48 CET 2009
try this:
> # create a factor and then convert back to numeric
> x$nb <- as.integer(factor(x$name, levels=unique(x$name))) + 99
> x
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:
> 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
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list