[R] identify an element in a column

Joshua Wiley jwiley.psych at gmail.com
Tue Feb 22 18:33:04 CET 2011


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 <pdxdong at gmail.com> 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]]
>
> ______________________________________________
> 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.
>



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



More information about the R-help mailing list