identify an element in a column

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

identify an element in a column

Hongwei Dong
Hi, R users,

I'm wondering if I can identify an element in a column by an element in
another column. For example:

x<-1:10
y<-11:20
z<-cbind(x,y)
z
     x  y
 [1,]  1 11
 [2,]  2 12
 [3,]  3 13
 [4,]  4 14
 [5,]  5 15
 [6,]  6 16
 [7,]  7 17
 [8,]  8 18
 [9,]  9 19
[10,] 10 20

What I want to do is: when x=5, y=y-1

Anyone can tell me how to do this? Thanks.


Gary

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: identify an element in a column

Erik Iverson-3
Is this what you mean?

z[which(z[,"x"] == 5) - 1, "y"]

?which is probably what you're looking for...

Hongwei Dong wrote:

> Hi, R users,
>
> I'm wondering if I can identify an element in a column by an element in
> another column. For example:
>
> x<-1:10
> y<-11:20
> z<-cbind(x,y)
> z
>      x  y
>  [1,]  1 11
>  [2,]  2 12
>  [3,]  3 13
>  [4,]  4 14
>  [5,]  5 15
>  [6,]  6 16
>  [7,]  7 17
>  [8,]  8 18
>  [9,]  9 19
> [10,] 10 20
>
> What I want to do is: when x=5, y=y-1
>
> Anyone can tell me how to do this? Thanks.
>
>
> Gary
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: identify an element in a column

D. Rizopoulos
In reply to this post by Hongwei Dong
try this:

x <- 1:10
y <- 11:20
z <- cbind(x, y)

ind <- x == 5
z[ind, "y"] <- z[ind, "y"] - 1
z


I hope it helps.

Best,
Dimitris


On 2/22/2011 6:18 PM, Hongwei Dong wrote:

> Hi, R users,
>
> I'm wondering if I can identify an element in a column by an element in
> another column. For example:
>
> x<-1:10
> y<-11:20
> z<-cbind(x,y)
> z
>       x  y
>   [1,]  1 11
>   [2,]  2 12
>   [3,]  3 13
>   [4,]  4 14
>   [5,]  5 15
>   [6,]  6 16
>   [7,]  7 17
>   [8,]  8 18
>   [9,]  9 19
> [10,] 10 20
>
> What I want to do is: when x=5, y=y-1
>
> Anyone can tell me how to do this? Thanks.
>
>
> Gary
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

--
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
Web: http://www.erasmusmc.nl/biostatistiek/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: identify an element in a column

Joshua Wiley-2
In reply to this post by Hongwei Dong
Hi Gary,

Another possibility besides Erik's (although I suspect his is what you
are really after):

## easier way to data
z <- cbind(x = 1:10, y = 11:20)
z[z[,"x"] == 5, "y"] - 1

## To see what is "going on", break it into pieces
## logical; does column 'x' of 'z' equal 5?
z[, "x"] == 5
## all values in column 'y'
z[ , "y"]
## subtract 1 from the values in column 'y'
z[ , "y"] - 1

## For documentation
?"["
?Logic

Cheers,

Josh

On Tue, Feb 22, 2011 at 9:18 AM, Hongwei Dong <[hidden email]> wrote:

> Hi, R users,
>
> I'm wondering if I can identify an element in a column by an element in
> another column. For example:
>
> x<-1:10
> y<-11:20
> z<-cbind(x,y)
> z
>     x  y
>  [1,]  1 11
>  [2,]  2 12
>  [3,]  3 13
>  [4,]  4 14
>  [5,]  5 15
>  [6,]  6 16
>  [7,]  7 17
>  [8,]  8 18
>  [9,]  9 19
> [10,] 10 20
>
> What I want to do is: when x=5, y=y-1
>
> Anyone can tell me how to do this? Thanks.
>
>
> Gary
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>



--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: identify an element in a column

Jorge I Velez
In reply to this post by Hongwei Dong
Hi Gary,

Try

transform(z, y = ifelse(x == 5, y-1, y))

HTH,
Jorge

On Tue, Feb 22, 2011 at 12:18 PM, Hongwei Dong <> wrote:

> Hi, R users,
>
> I'm wondering if I can identify an element in a column by an element in
> another column. For example:
>
> x<-1:10
> y<-11:20
> z<-cbind(x,y)
> z
>     x  y
>  [1,]  1 11
>  [2,]  2 12
>  [3,]  3 13
>  [4,]  4 14
>  [5,]  5 15
>  [6,]  6 16
>  [7,]  7 17
>  [8,]  8 18
>  [9,]  9 19
> [10,] 10 20
>
> What I want to do is: when x=5, y=y-1
>
> Anyone can tell me how to do this? Thanks.
>
>
> Gary
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: identify an element in a column

Ivan Calandra
In reply to this post by Erik Iverson-3
Another way is with ifelse:
z<-data.frame(x,y)
z$y2 <- ifelse(z$x==5,z$y-1,z$y)

HTH,
Ivan

Le 2/22/2011 18:27, Erik Iverson a écrit :

> Is this what you mean?
>
> z[which(z[,"x"] == 5) - 1, "y"]
>
> ?which is probably what you're looking for...
>
> Hongwei Dong wrote:
>> Hi, R users,
>>
>> I'm wondering if I can identify an element in a column by an element in
>> another column. For example:
>>
>> x<-1:10
>> y<-11:20
>> z<-cbind(x,y)
>> z
>>      x  y
>>  [1,]  1 11
>>  [2,]  2 12
>>  [3,]  3 13
>>  [4,]  4 14
>>  [5,]  5 15
>>  [6,]  6 16
>>  [7,]  7 17
>>  [8,]  8 18
>>  [9,]  9 19
>> [10,] 10 20
>>
>> What I want to do is: when x=5, y=y-1
>>
>> Anyone can tell me how to do this? Thanks.
>>
>>
>> Gary
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[hidden email]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

______________________________________________
[hidden email] 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.