[R] replacing values in a vector
Greg Snow
Greg.Snow at imail.org
Wed Aug 18 23:16:58 CEST 2010
What do you plan to do with those vectors?
Creating a set of vectors and 'asign'ing them as global variables will likely make future tasks harder than they need to be. In the long run it will probably work better for you to either keep them as a matrix (and just access the row/column you need) or put them into a single list and access the elements of that.
--
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 Karen Kotschy
> Sent: Tuesday, August 17, 2010 4:37 PM
> To: Dimitris Rizopoulos
> Cc: r-help at r-project.org
> Subject: Re: [R] replacing values in a vector
>
> Thanks so much to all those who suggested solutions! You guys are
> wonderful!
>
> I went with Dimitris' suggestion in the end.
>
> Regards
> Karen
>
> On Tue 17Aug10, Dimitris Rizopoulos wrote:
> > try this:
> >
> > vec <- c(3,2,6,4,7)
> >
> > n <- length(vec)
> > for(i in seq_along(vec)){
> > r <- numeric(n)
> > r[i] <- vec[i]
> > assign(paste("vec.", letters[i], sep = ""), r)
> > }
> >
> >
> > I hope it helps.
> >
> > Best,
> > Dimitris
> >
> >
> > On 8/17/2010 12:57 PM, Karen Kotschy wrote:
> > >Dear helpRs
> > >
> > >Does anyone have an elegant way of doing the following:
> > >
> > >For a given numeric vector, e.g. vec<- c(3,2,6,4,7)
> > >
> > >Create a series of vectors where all but 1 of the values are
> replaced by
> > >0's, e.g.
> > >
> > >vec.a<- c(3,0,0,0,0)
> > >vec.b<- c(0,2,0,0,0)
> > >vec.c<- c(0,0,6,0,0)
> > >vec.d<- c(0,0,0,4,0)
> > >vec.e<- c(0,0,0,0,7)
> > >
> > >I have looked at `replace', but can't think of a way of making it
> produce
> > >the 5 lines above without a for loop.
> > >
> > >I would also like to assign the names automatically. I can create
> them
> > >easily using paste, but how does one get R to treat the resulting
> > >character strings as object names to which values can be assigned?
> > >
> > >Thanks!
> > >Karen
> > >
> > >---
> > >Karen Kotschy
> > >Centre for Water in the Environment
> > >University of the Witwatersrand, Johannesburg, South Africa
> > >
> > >
> >
> > --
> > Dimitris Rizopoulos
> > Assistant Professor
> > Department of Biostatistics
> > Erasmus University Medical Center
> >
> > Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
> > Tel: +31/(0)10/7043478
> > Fax: +31/(0)10/7043014
> >
> > --
> > This message has been scanned for viruses and
> > dangerous content by MailScanner, and is
> > believed to be clean.
> >
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list