On 5/13/2008 10:27 AM, Yukihiro Ishii wrote:> Hi Rusers! > > I am ashed of asking such a simple question. > > X<-matrix(rnorm(24), 4) > X0<-apply(X,2,mean) > > What I want is a matrix which consists of colums such as X[,1]--X0[1]. > > X-X0 doesn't work. > > Perhaps apply function?scale(X, scale=FALSE) ?scale> Thanks in advance. > > Yukihiro Ishii > 2-3-28 Tsurumakiminami, Hadano, 250-0002 Japan > +81463691922 > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
?sweep -----Original Message----- From: "Yukihiro Ishii" <yukiasais at ybb.ne.jp> To: "r-help at r-project.org" <r-help at r-project.org> Sent: 5/13/08 8:30 AM Subject: [R] A Very Simple Question Hi Rusers! I am ashed of asking such a simple question. X<-matrix(rnorm(24), 4) X0<-apply(X,2,mean) What I want is a matrix which consists of colums such as X[,1]--X0[1]. X-X0 doesn't work. Perhaps apply function? Thanks in advance. Yukihiro Ishii 2-3-28 Tsurumakiminami, Hadano, 250-0002 Japan +81463691922 ______________________________________________ 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.
See ?scale HTH, Giovanni> Date: Tue, 13 May 2008 23:27:59 +0900 > From: Yukihiro Ishii <yukiasais at ybb.ne.jp> > Sender: r-help-bounces at r-project.org > Precedence: list > DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=ybb20050223; d=ybb.ne.jp; > > Hi Rusers! > > I am ashed of asking such a simple question. > > X<-matrix(rnorm(24), 4) > X0<-apply(X,2,mean) > > What I want is a matrix which consists of colums such as X[,1]--X0[1]. > > X-X0 doesn't work. > > Perhaps apply function? > > Thanks in advance. > > Yukihiro Ishii > 2-3-28 Tsurumakiminami, Hadano, 250-0002 Japan > +81463691922 > > ______________________________________________ > 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. > >-- Giovanni Petris <GPetris at uark.edu> Associate Professor Department of Mathematical Sciences University of Arkansas - Fayetteville, AR 72701 Ph: (479) 575-6324, 575-8630 (fax) http://definetti.uark.edu/~gpetris/
Hi R, Suppose l=c(1,1,1,2,2,1,1,1) k[-which(k==1)] [1] 2 2 k[-which(k==2)] [1] 1 1 1 1 1 1 But, k[-which(k==3)] numeric(0) I do not want this numeric(0), instead the whole k itself should be my result... How do I do this? Thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}}
On 5/14/2008 11:16 AM, Shubha Vishwanath Karanth wrote:> Hi R, > > > > Suppose > > l=c(1,1,1,2,2,1,1,1) > > > > k[-which(k==1)] > > [1] 2 2 > > > > k[-which(k==2)] > > [1] 1 1 1 1 1 1 > > > > But, > > > > k[-which(k==3)] > > numeric(0) > > > > I do not want this numeric(0), instead the whole k itself should be my > result... How do I do this? > > > > > > Thanks, > > ShubhaLook at the result of which(k==3) to see why your approach does not work. You might try this instead: k <- c(1,1,1,2,2,1,1,1) k[!(k==1)] [1] 2 2 k[!(k==2)] [1] 1 1 1 1 1 1 k[!(k==3)] [1] 1 1 1 2 2 1 1 1> This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
> k <- c(1,1,1,2,2,1,1,1) > k[k != 1][1] 2 2> k[k != 2][1] 1 1 1 1 1 1> k[k != 3][1] 1 1 1 2 2 1 1 1>-Christos> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Shubha > Vishwanath Karanth > Sent: Wednesday, May 14, 2008 11:16 AM > To: r-help at stat.math.ethz.ch > Subject: [R] A very simple question > > Hi R, > > > > Suppose > > l=c(1,1,1,2,2,1,1,1) > > > > k[-which(k==1)] > > [1] 2 2 > > > > k[-which(k==2)] > > [1] 1 1 1 1 1 1 > > > > But, > > > > k[-which(k==3)] > > numeric(0) > > > > I do not want this numeric(0), instead the whole k itself > should be my result... How do I do this? > > > > > > Thanks, > > Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > ______________________________________________ > 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. > >
Use logical subscripts rather than which:> k <- c(1,1,1,2,2,1,1,1) > k[ k != 1 ][1] 2 2> k[ k != 2 ][1] 1 1 1 1 1 1> k[ k != 3 ][1] 1 1 1 2 2 1 1 1 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 Shubha > Vishwanath Karanth > Sent: Wednesday, May 14, 2008 9:16 AM > To: r-help at stat.math.ethz.ch > Subject: [R] A very simple question > > Hi R, > > > > Suppose > > l=c(1,1,1,2,2,1,1,1) > > > > k[-which(k==1)] > > [1] 2 2 > > > > k[-which(k==2)] > > [1] 1 1 1 1 1 1 > > > > But, > > > > k[-which(k==3)] > > numeric(0) > > > > I do not want this numeric(0), instead the whole k itself > should be my result... How do I do this? > > > > > > Thanks, > > Shubha > > > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > ______________________________________________ > 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. >
Try this: k=c(1,1,1,2,2,1,1,1) > k[(k!=1)] [1] 2 2 > k[(k!=2)] [1] 1 1 1 1 1 1 > k[(k!=3)] [1] 1 1 1 2 2 1 1 1 Julian Shubha Vishwanath Karanth wrote:> Hi R, > > > > Suppose > > l=c(1,1,1,2,2,1,1,1) > > > > k[-which(k==1)] > > [1] 2 2 > > > > k[-which(k==2)] > > [1] 1 1 1 1 1 1 > > > > But, > > > > k[-which(k==3)] > > numeric(0) > > > > I do not want this numeric(0), instead the whole k itself should be my > result... How do I do this? > > > > > > Thanks, > > Shubha > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > 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.
Or> k <- c(1,1,1,2,2,1,1) > k[!(k==3)][1] 1 1 1 2 2 1 1>On Wed, May 14, 2008 at 1:59 PM, Julian Burgos <jmburgos at u.washington.edu> wrote:> Try this: > > k=c(1,1,1,2,2,1,1,1) > >> k[(k!=1)] > [1] 2 2 > >> k[(k!=2)] > [1] 1 1 1 1 1 1 > >> k[(k!=3)] > [1] 1 1 1 2 2 1 1 1 > > Julian > > > Shubha Vishwanath Karanth wrote: >> >> Hi R, >> >> >> Suppose >> >> l=c(1,1,1,2,2,1,1,1) >> >> >> k[-which(k==1)] >> >> [1] 2 2 >> >> >> k[-which(k==2)] >> >> [1] 1 1 1 1 1 1 >> >> >> But, >> >> >> k[-which(k==3)] >> >> numeric(0) >> >> >> I do not want this numeric(0), instead the whole k itself should be my >> result... How do I do this? >> >> >> >> Thanks, >> >> Shubha >> >> >> This e-mail may contain confidential and/or privileged i...{{dropped:13}} >> >> ______________________________________________ >> 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. > > ______________________________________________ > 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. >-- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
There have been several solutions like: k[k != 3] The more general form of this idea is: k[!(k %in% 3)] Sticking closer to the original form would be: out <- which(k == 3) if(length(out)) k[-out] else k Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Shubha Vishwanath Karanth wrote:> Hi R, > > > > Suppose > > l=c(1,1,1,2,2,1,1,1) > > > > k[-which(k==1)] > > [1] 2 2 > > > > k[-which(k==2)] > > [1] 1 1 1 1 1 1 > > > > But, > > > > k[-which(k==3)] > > numeric(0) > > > > I do not want this numeric(0), instead the whole k itself should be my > result... How do I do this? > > > > > > Thanks, > > Shubha > > > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > 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. > > >