ElMaestro
★★★

Denmark,
2016-09-21 14:59
(3137 d 20:06 ago)

Posting: # 16657
Views: 5,823
 

 Smart recoding of vector values [🇷 for BE/BA]

Hi all, especially the R experts out there,

I have a vector like
a=c(1,1,2,2,3,5,5,6,6,7,8,8,9,9)
and I would like to rename/reorder/resomething this vector so that
a=c(1,1,2,2,3,4,4,5,5,6,7,7,8,8)

They are actually factor levels.

So far I took a look at reorder, rank, and sort and googled around but I am not really getting anywhere. I have written a clumsy function that does the trick, but I am sure this can be achieved with a one-liner somehow.

Anyone able to tell how?

Thanks a lot in advance and have a great day.:-)

Pass or fail!
ElMaestro
d_labes
★★★

Berlin, Germany,
2016-09-21 15:32
(3137 d 19:33 ago)

@ ElMaestro
Posting: # 16659
Views: 4,742
 

 Vectorized if

Dear öberster größter Meister!

❝ I have a vector like

a=c(1,1,2,2,3,5,5,6,6,7,8,8,9,9)

❝ and I would like to rename/reorder/resomething this vector so that

a=c(1,1,2,2,3,4,4,5,5,6,7,7,8,8)


Vectorized if comes to rescue.
a <- ifelse(a<4, a, a-1)

Hope this helps and matches your problem.

BTW: Hardcore Rusers never use = for assignments :no:

Regards,

Detlew
mittyri
★★  

Russia,
2016-09-21 15:34
(3137 d 19:31 ago)

@ ElMaestro
Posting: # 16660
Views: 4,694
 

 Smart recoding of vector values

Hi ElMaestro,

my noob version
a <- c(1,1,2,2,3,5,5,6,6,7,8,8,9,9)
a <- as.integer(factor(a, unique(a)))

Kind regards,
Mittyri
ElMaestro
★★★

Denmark,
2016-09-21 16:02
(3137 d 19:03 ago)

@ mittyri
Posting: # 16661
Views: 4,740
 

 Smart recoding of vector values

Dear Mittyri and d_labes,


❝ my noob version

a <- c(1,1,2,2,3,5,5,6,6,7,8,8,9,9)

a <- as.integer(factor(a, unique(a)))


Thanks a lot to both of you. You guys are nothing short of geniuses.
I think Mityri's example is better. The example that I gave had one single missing integer, but there are frequently plenty, like
a=c(1,1,2,2,3,5,5,6,6,7,8,8,12,12, 13,13,14,16,16) etc. I apologise I should have written that.



a<-5
if(2*a<-<-10) cat("Yah, that '<-' business is very neat.\n")
:-D:-D:-D

Pass or fail!
ElMaestro
d_labes
★★★

Berlin, Germany,
2016-09-21 17:27
(3137 d 17:38 ago)

@ mittyri
Posting: # 16662
Views: 4,722
 

 Smart recoding of vector values

Dear mittyri,

a <- c(1,1,2,2,3,5,5,6,6,7,8,8,9,9)

a <- as.integer(factor(a, unique(a)))


Smart :ok:

Regards,

Detlew
UA Flag
Activity
 Admin contact
23,424 posts in 4,927 threads, 1,668 registered users;
34 visitors (0 registered, 34 guests [including 4 identified bots]).
Forum time: 11:05 CEST (Europe/Vienna)

It is a well-known experience that the only truly enjoyable and
profitable way of studying mathematics is the method of
“filling in details” by one’s own efforts.    Cornelius Lanczos

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5