Re: [R] Selecting rows according to a column

2009-10-28 Thread Peter Ehlers


William Dunlap wrote:

-Original Message-
From: r-help-boun...@r-project.org 
[mailto:r-help-boun...@r-project.org] On Behalf Of Erik Iverson

Sent: Wednesday, October 28, 2009 9:22 AM
To: Gurpal Kalsi; r-help@r-project.org
Subject: Re: [R] Selecting rows according to a column

Hello,

Here's an idea: 


ifelse(z$c == "a", z$a, z$b)


If there may be many columns to select from (so
the nested ifelse's become tedious) one might try
   > z[,-3][cbind(seq_len(nrow(z)),z$c)]
   [1]  1  2 30 40 50
The initial [,-3] is there only so when the data.frame
is converted to a matrix by [.data.frame's processing
of a matrix subscript it becomes a numeric matrix, not
a character matrix.


Now _that's_ elegant!!

 -Peter Ehlers


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  




-Original Message-
From: r-help-boun...@r-project.org 

[mailto:r-help-boun...@r-project.org]

On Behalf Of Gurpal Kalsi
Sent: Wednesday, October 28, 2009 11:15 AM
To: r-help@r-project.org
Subject: [R] Selecting rows according to a column

Hi,

With a data such as:

z = data.frame(a = 1:5, b=10*a, c = c("a", "a", "b", "b", "b") )

* a  b  c*
 *1* 10 *a*
 *2* 20 *a*
 3 *30* *b*
 4 *40* *b*
 5 *50* *b*

Can anyone suggest a way to select [1, 2, 30, 40, 50],
ie. using column "c" to specify which column is selected 

for each row.

Many thanks

G

[[alternative HTML version deleted]]

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



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


Re: [R] Selecting rows according to a column

2009-10-28 Thread Gurpal Kalsi
Thanks very much!

Gurpal
Centrica Energy.

On Wed, Oct 28, 2009 at 4:31 PM, William Dunlap  wrote:

> > -Original Message-
> > From: r-help-boun...@r-project.org
> > [mailto:r-help-boun...@r-project.org] On Behalf Of Erik Iverson
> > Sent: Wednesday, October 28, 2009 9:22 AM
> > To: Gurpal Kalsi; r-help@r-project.org
> > Subject: Re: [R] Selecting rows according to a column
> >
> > Hello,
> >
> > Here's an idea:
> >
> > ifelse(z$c == "a", z$a, z$b)
>
> If there may be many columns to select from (so
> the nested ifelse's become tedious) one might try
>   > z[,-3][cbind(seq_len(nrow(z)),z$c)]
>   [1]  1  2 30 40 50
> The initial [,-3] is there only so when the data.frame
> is converted to a matrix by [.data.frame's processing
> of a matrix subscript it becomes a numeric matrix, not
> a character matrix.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
> >
> >
> > > -Original Message-
> > > From: r-help-boun...@r-project.org
> > [mailto:r-help-boun...@r-project.org]
> > > On Behalf Of Gurpal Kalsi
> > > Sent: Wednesday, October 28, 2009 11:15 AM
> > > To: r-help@r-project.org
> > > Subject: [R] Selecting rows according to a column
> > >
> > > Hi,
> > >
> > > With a data such as:
> > > > z = data.frame(a = 1:5, b=10*a, c = c("a", "a", "b", "b", "b") )
> > > * a  b  c*
> > >  *1* 10 *a*
> > >  *2* 20 *a*
> > >  3 *30* *b*
> > >  4 *40* *b*
> > >  5 *50* *b*
> > >
> > > Can anyone suggest a way to select [1, 2, 30, 40, 50],
> > > ie. using column "c" to specify which column is selected
> > for each row.
> > >
> > > Many thanks
> > >
> > > G
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > __
> > > 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.
> >
>

[[alternative HTML version deleted]]

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


Re: [R] Selecting rows according to a column

2009-10-28 Thread William Dunlap
> -Original Message-
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of Erik Iverson
> Sent: Wednesday, October 28, 2009 9:22 AM
> To: Gurpal Kalsi; r-help@r-project.org
> Subject: Re: [R] Selecting rows according to a column
> 
> Hello,
> 
> Here's an idea: 
> 
> ifelse(z$c == "a", z$a, z$b)

If there may be many columns to select from (so
the nested ifelse's become tedious) one might try
   > z[,-3][cbind(seq_len(nrow(z)),z$c)]
   [1]  1  2 30 40 50
The initial [,-3] is there only so when the data.frame
is converted to a matrix by [.data.frame's processing
of a matrix subscript it becomes a numeric matrix, not
a character matrix.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> 
> 
> > -Original Message-
> > From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org]
> > On Behalf Of Gurpal Kalsi
> > Sent: Wednesday, October 28, 2009 11:15 AM
> > To: r-help@r-project.org
> > Subject: [R] Selecting rows according to a column
> > 
> > Hi,
> > 
> > With a data such as:
> > > z = data.frame(a = 1:5, b=10*a, c = c("a", "a", "b", "b", "b") )
> > * a  b  c*
> >  *1* 10 *a*
> >  *2* 20 *a*
> >  3 *30* *b*
> >  4 *40* *b*
> >  5 *50* *b*
> > 
> > Can anyone suggest a way to select [1, 2, 30, 40, 50],
> > ie. using column "c" to specify which column is selected 
> for each row.
> > 
> > Many thanks
> > 
> > G
> > 
> > [[alternative HTML version deleted]]
> > 
> > __
> > 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.
> 

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


Re: [R] Selecting rows according to a column

2009-10-28 Thread Adaikalavan Ramasamy

Not very elegant but try:

 z <- data.frame(a = 1:5, b=10*(1:5), c = c("a", "a", "b", "b", "b") )
 z[ cbind( 1:nrow(z), match( as.character(z$c) , colnames(z) ) ) ]

If you have very few columns, you can use ifelse() too.

Regards, Adai



Gurpal Kalsi wrote:

Hi,

With a data such as:

z = data.frame(a = 1:5, b=10*a, c = c("a", "a", "b", "b", "b") )

* a  b  c*
 *1* 10 *a*
 *2* 20 *a*
 3 *30* *b*
 4 *40* *b*
 5 *50* *b*

Can anyone suggest a way to select [1, 2, 30, 40, 50],
ie. using column "c" to specify which column is selected for each row.

Many thanks

G

[[alternative HTML version deleted]]

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


Re: [R] Selecting rows according to a column

2009-10-28 Thread Erik Iverson
Hello,

Here's an idea: 

ifelse(z$c == "a", z$a, z$b)


> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
> On Behalf Of Gurpal Kalsi
> Sent: Wednesday, October 28, 2009 11:15 AM
> To: r-help@r-project.org
> Subject: [R] Selecting rows according to a column
> 
> Hi,
> 
> With a data such as:
> > z = data.frame(a = 1:5, b=10*a, c = c("a", "a", "b", "b", "b") )
> * a  b  c*
>  *1* 10 *a*
>  *2* 20 *a*
>  3 *30* *b*
>  4 *40* *b*
>  5 *50* *b*
> 
> Can anyone suggest a way to select [1, 2, 30, 40, 50],
> ie. using column "c" to specify which column is selected for each row.
> 
> Many thanks
> 
> G
> 
>   [[alternative HTML version deleted]]
> 
> __
> 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.


[R] Selecting rows according to a column

2009-10-28 Thread Gurpal Kalsi
Hi,

With a data such as:
> z = data.frame(a = 1:5, b=10*a, c = c("a", "a", "b", "b", "b") )
* a  b  c*
 *1* 10 *a*
 *2* 20 *a*
 3 *30* *b*
 4 *40* *b*
 5 *50* *b*

Can anyone suggest a way to select [1, 2, 30, 40, 50],
ie. using column "c" to specify which column is selected for each row.

Many thanks

G

[[alternative HTML version deleted]]

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