A Very Simple Question

View: New views
11 Messages — Rating Filter:   Alert me  

A Very Simple Question

by yishii :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@... 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.

Re: A Very Simple Question

by Chuck Cleland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@... 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

______________________________________________
R-help@... 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.

Re: A Very Simple Question

by Giovanni Petris :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


See

?scale

HTH,
Giovanni

> Date: Tue, 13 May 2008 23:27:59 +0900
> From: Yukihiro Ishii <yukiasais@...>
> Sender: r-help-bounces@...
> 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@... 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@...>
Associate Professor
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/

______________________________________________
R-help@... 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.

Parent Message unknown A very simple question

by Shubha Vishwanath Karanth :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@... 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.

Re: A very simple question

by Christos Hatzis-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> 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@...
> [mailto:r-help-bounces@...] On Behalf Of Shubha
> Vishwanath Karanth
> Sent: Wednesday, May 14, 2008 11:16 AM
> To: r-help@...
> 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@... 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@... 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.

Re: A very simple question

by Chuck Cleland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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,
>
> Shubha

   Look 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@... 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

______________________________________________
R-help@... 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.

Re: A very simple question

by Ingmar Visser :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

l[!l==3]

On 14 May 2008, at 17:16, 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@... 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.

Ingmar Visser
Department of Psychology, University of Amsterdam
Roetersstraat 15
1018 WB Amsterdam
The Netherlands
t: +31-20-5256723



        [[alternative HTML version deleted]]

______________________________________________
R-help@... 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.

Re: A very simple question

by Greg Snow-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
(801) 408-8111



> -----Original Message-----
> From: r-help-bounces@...
> [mailto:r-help-bounces@...] On Behalf Of Shubha
> Vishwanath Karanth
> Sent: Wednesday, May 14, 2008 9:16 AM
> To: r-help@...
> 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@... 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@... 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.

Re: A very simple question

by Julian Burgos :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@... 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@... 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.

Re: A very simple question

by Erin Hodgess-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> 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@... 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@... 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@...

______________________________________________
R-help@... 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.

Re: A very simple question

by Patrick Burns :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
+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@... 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@... 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.