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
Help with indexing
3 messages · Dana Sevak, William Dunlap, jim holtman
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Dana Sevak
Sent: Saturday, November 21, 2009 4:00 PM
To: r-help at r-project.org
Subject: [R] Help with indexing
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?
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
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.
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?