Christian Langkamp
2009-Feb-18 21:24 UTC
[R] Re place Values within vector using Translation vector
Dear everyone I would like to change values in vectors doing a translation. i.e. I have a start vector giving me the levels in one vector (numbers 1 to x - rating) and then I have a second vector giving me the values to be allocated (loss probabilities), but the number of potential rating classes and loss estimates is still subject to a lot of discussion. Attached a simplified version of the problem (original has more values and needs to stay flexible, i.e. length of translation vector can change. Trans_Prob_values<-c(0.005, 0.01, 0.1) Trans_CR<-c(1,2,3) a<-c(3,2,1,1,2,3) A<-replace(a, Trans_CR, Trans_Prob_values) A This however produces [1] 0.005 0.010 0.100 1.000 2.000 3.000 as opposed to the desired result. The help however says "replace replaces the values in x with indexes given in list by those given in values. If necessary, the values in values are recycled. " which in my view should be exactly doing the job intended above. Constructions with nested ifelse statements and individual replacements are too cumbersome in my view. I searched for conditional replacement, vector replace, replace function and read the problems, but generally they have conditions like age>30 then x, not a direct translation of values. If anyone has an idea, please do share it. Thanks Christian -- View this message in context: http://www.nabble.com/Replace-Values-within-vector-using-Translation-vector-tp22088527p22088527.html Sent from the R help mailing list archive at Nabble.com.
Duncan Murdoch
2009-Feb-18 21:30 UTC
[R] Re place Values within vector using Translation vector
On 18/02/2009 4:24 PM, Christian Langkamp wrote:> Dear everyone > I would like to change values in vectors doing a translation. i.e. I have a > start vector giving me the levels in one vector (numbers 1 to x - rating) > and then I have a second vector giving me the values to be allocated (loss > probabilities), but the number of potential rating classes and loss > estimates is still subject to a lot of discussion. > > Attached a simplified version of the problem (original has more values and > needs to stay flexible, i.e. length of translation vector can change. > > Trans_Prob_values<-c(0.005, 0.01, 0.1) > Trans_CR<-c(1,2,3) > a<-c(3,2,1,1,2,3) > A<-replace(a, Trans_CR, Trans_Prob_values) > A > > This however produces > [1] 0.005 0.010 0.100 1.000 2.000 3.000 > as opposed to the desired result. > > > The help however says > "replace replaces the values in x with indexes given in listSince your Trans_CR contains 1:3, this says the first 3 entries of x will be replaced, and that's what happened. What you want is simply A <- Trans_Prob_values[a] (assuming that the indices are always in 1:x, and that Trans_Prob_values will have x values in it). Duncan Murdoch by those given> in values. If necessary, the values in values are recycled. " which in my > view should be exactly doing the job intended above. > > Constructions with nested ifelse statements and individual replacements are > too cumbersome in my view. > I searched for conditional replacement, vector replace, replace function and > read the problems, but generally they have conditions like age>30 then x, > not a direct translation of values. > > If anyone has an idea, please do share it. > Thanks > Christian >
Greg Snow
2009-Feb-18 21:34 UTC
[R] Re place Values within vector using Translation vector
The call to replace is replacing the 1st 3 elements of a (your indexes in Trans_CR) with the values and leaving the 4-6 elements alone. For what you want, try: A <- Trans_Prob_values[ match(a, Trans_CR) ] Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Christian Langkamp > Sent: Wednesday, February 18, 2009 2:25 PM > To: r-help at r-project.org > Subject: [R] Re place Values within vector using Translation vector > > > Dear everyone > I would like to change values in vectors doing a translation. i.e. I > have a > start vector giving me the levels in one vector (numbers 1 to x - > rating) > and then I have a second vector giving me the values to be allocated > (loss > probabilities), but the number of potential rating classes and loss > estimates is still subject to a lot of discussion. > > Attached a simplified version of the problem (original has more values > and > needs to stay flexible, i.e. length of translation vector can change. > > Trans_Prob_values<-c(0.005, 0.01, 0.1) > Trans_CR<-c(1,2,3) > a<-c(3,2,1,1,2,3) > A<-replace(a, Trans_CR, Trans_Prob_values) > A > > This however produces > [1] 0.005 0.010 0.100 1.000 2.000 3.000 > as opposed to the desired result. > > > The help however says > "replace replaces the values in x with indexes given in list by those > given > in values. If necessary, the values in values are recycled. " which in > my > view should be exactly doing the job intended above. > > Constructions with nested ifelse statements and individual replacements > are > too cumbersome in my view. > I searched for conditional replacement, vector replace, replace > function and > read the problems, but generally they have conditions like age>30 then > x, > not a direct translation of values. > > If anyone has an idea, please do share it. > Thanks > Christian > > -- > View this message in context: http://www.nabble.com/Replace-Values- > within-vector-using-Translation-vector-tp22088527p22088527.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.