HI Brian, If ?names() work for you, then: x <- rnorm(10) myindex <- seq(from = 1,to = 20, by = 2) myindex1<-seq(from=1,to=30,by=3)
fun1<-function(vec1,indx){ y<-numeric() y<-na.omit(y[indx]<-vec1) names(y)<-indx y} fun1(x,myindex) # 1 3 5 7 9 11 #0.430819171 0.733928361 -0.680674874 0.326196338 0.907029629 -0.462681535 # 13 15 17 19 #0.004700029 1.450397255 0.752679093 0.966159790 fun1(x,myindex1) # 1 4 7 10 13 16 # 0.430819171 0.733928361 -0.680674874 0.326196338 0.907029629 -0.462681535 # 19 22 25 28 #0.004700029 1.450397255 0.752679093 0.966159790 A.K. ----- Original Message ----- From: Brian Feeny <bfe...@mac.com> To: arun <smartpink...@yahoo.com> Cc: R help <r-help@r-project.org>; David Winsemius <dwinsem...@comcast.net> Sent: Thursday, December 6, 2012 12:44 AM Subject: Re: [R] Assignment of values with different indexes No, because it does not assign the indexes of myindex. If its not possible, which I am assuming its not, thats OK. I thought that if I had say 10 observations, sequentially ordered (or any order, it doesn't matter), and I wanted to assign them specific indexes, and not have NA's, that it was possible. I am OK with knowing that I can assign them the specific indexes, and that there will be empty spots, which are marked NA. Most functions I would need to use can handle NA's by telling the function to ignore. I appreciate all the help that has been given. Brian On Dec 5, 2012, at 11:49 PM, arun <smartpink...@yahoo.com> wrote: > > > Hi, > > Would it be okay to use: > y<-na.omit(y[myindex]<-x) > y > # [1] -1.36025132 -0.57529211 1.18132359 0.41038489 1.83108252 -0.03563686 > #[7] 1.25267314 1.08311857 1.56973422 -0.30752939 > > A.K. > > > ----- Original Message ----- > From: Brian Feeny <bfe...@mac.com> > To: "r-help@r-project.org help" <r-help@r-project.org> > Cc: > Sent: Wednesday, December 5, 2012 9:47 PM > Subject: [R] Assignment of values with different indexes > > > I would like to take the values of observations and map them to a new index. > I am not sure how to accomplish this. The result would look like so: > > x[1,2,3,4,5,6,7,8,9,10] > becomes > y[2,4,6,8,10,12,14,16,18,20] > > The "newindex" would not necessarily be this sequence, but a sequence I have > stored in a vector, so it could be all kinds of values. here is what happens: > >> x <- rnorm(10) >> myindex <- seq(from = 1,to = 20, by = 2) >> y <- numeric() >> y[myindex] <- x >> y > [1] -0.03745988 NA -0.09078822 NA 0.92484413 NA > 0.32057426 NA > [9] 0.01536279 NA 0.02200198 NA 0.37535438 NA > 1.46606535 NA > [17] 1.44855796 NA -0.05048738 > > So yes, it maps the values to my new indexes, but I have NA's. The result I > want would look like this instead: > > > [1] -0.03745988 > [3] -0.09078822 > [5] 0.92484413 > [7] 0.32057426 > [9] 0.01536279 > [11] 0.02200198 > [13] 0.37535438 > [15] 1.46606535 > [17] 1.44855796 > [19] -0.05048738 > > > and remove the NA's. I tried this with na.omit() on x, but it looks like so: > >> x <- rnorm(10) >> myindex <- seq(from = 1,to = 20, by = 2) >> y <- numeric() >> y[myindex] <- na.omit(x) >> y > [1] 0.87399523 NA -0.39908184 NA 0.14583051 NA > 0.01850755 NA > [9] -0.47413632 NA 0.88410517 NA -1.64939190 NA > 0.57650807 NA > [17] 0.44016971 NA -0.56313802 > > Brian > > ______________________________________________ > R-help@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. > ______________________________________________ R-help@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.