Here's another one: match(d, unique(d)).
Arun
From:Â Greg Snow 538...@gmail.com
Reply:Â Greg Snow 538...@gmail.com
Date:Â March 12, 2014 at 8:41:31 PM
To:Â T Bal studentt...@gmail.com
Cc:Â r-help r-help@r-project.org
Subject:Â Re: [R] Assign numbers in R
Here are a couple more optio
Here are a couple more options if you want some variety:
> d <- c(8,7,5,5,3,3,2,1,1,1)
> as.numeric( factor(d, levels=unique(d)) )
[1] 1 2 3 3 4 4 5 6 6 6
> cumsum( !duplicated(d) )
[1] 1 2 3 3 4 4 5 6 6 6
What would you want the output to be if your d vector had another 8
after the last 1? T
No, this is not a homework. Thank you so much!
On Wed, Mar 12, 2014 at 10:39 AM, Kehl Dániel wrote:
> Hi,
>
> is this homework?
> Try
> d <- c(8,7,5,5,3,3,2,1,1,1)
>
> r <- rep(1,length(d))
>
> for (i in 2:length(d)) {
>
> if (d[i] != d[i-1]) {
> r[i]=r[i-1]+1;
> }
> else {
> r[i]
Hi,
Try:
cumsum(c(TRUE,d[-1]!=d[-length(d)]))
A.K.
On Wednesday, March 12, 2014 5:28 AM, T Bal wrote:
Hi,
I have the following numbers:
d <- c(8,7,5,5,3,3,2,1,1,1)
I want to convert these into the following numbers:
r:
1,2,3,3,4,4,5,6,6,6
So if two numbers are different increment it if t
Hi,
is this homework?
Try
d <- c(8,7,5,5,3,3,2,1,1,1)
r <- rep(1,length(d))
for (i in 2:length(d)) {
if (d[i] != d[i-1]) {
r[i]=r[i-1]+1;
}
else {
r[i] = r[i-1];
}
}
Although I am sure there are better solutions!
HTH
daniel
Feladó:
Hello,
Try the following.
r <- cumsum(c(TRUE, diff(d) != 0))
Hope this helps,
Rui Barradas
Em 12-03-2014 09:13, T Bal escreveu:
Hi,
I have the following numbers:
d <- c(8,7,5,5,3,3,2,1,1,1)
I want to convert these into the following numbers:
r:
1,2,3,3,4,4,5,6,6,6
So if two numbers are
Hello,
For your example, the following will work:
R> d <- c(8,7,5,5,3,3,2,1,1,1)
R> idx <- 1:length(unique(d))
R> rep(idx, rle(d)$length)
[1] 1 2 3 3 4 4 5 6 6 6
HTH,
Pascal
On Wed, Mar 12, 2014 at 6:13 PM, T Bal wrote:
> Hi,
> I have the following numbers:
>
> d <- c(8,7,5,5,3,3,2,1,1,1)
>
>
7 matches
Mail list logo