[R] selecting elements in vector
Hi everybody, I have the following problem. I have a vector containing character elements, such as: list = c(aa,bb,cc,dd,ee) I want to create an index which identifies the elements that are different from, e.g. aa and bb. When I do the following: jj = list!=aa list!=bb jj [1] FALSE FALSE TRUE TRUE TRUE I do the job. But I wonder whether there is a shorter command to compare the elements of 'list' directly to both aa and bb, avoiding the repetition of ' list!=...' and get a result such as jj thanks! -- Andrea Mario Lavezzi currently visiting the University of Oxford, Nuffield College Dipartimento Studi su Politica, Diritto e Società Piazza Bologni 8 90134 Palermo, Italy tel. ++39 091 23892208 fax ++39 091 6112023 skype: lavezzimario email: lave...@unipa.it web: http://www.unipa.it/~lavezzi [[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 elements in vector
Try this: !list %in% c(aa, bb) On Fri, Jan 14, 2011 at 10:19 AM, A M Lavezzi lave...@unipa.it wrote: Hi everybody, I have the following problem. I have a vector containing character elements, such as: list = c(aa,bb,cc,dd,ee) I want to create an index which identifies the elements that are different from, e.g. aa and bb. When I do the following: jj = list!=aa list!=bb jj [1] FALSE FALSE TRUE TRUE TRUE I do the job. But I wonder whether there is a shorter command to compare the elements of 'list' directly to both aa and bb, avoiding the repetition of ' list!=...' and get a result such as jj thanks! -- Andrea Mario Lavezzi currently visiting the University of Oxford, Nuffield College Dipartimento Studi su Politica, Diritto e SocietÄ Piazza Bologni 8 90134 Palermo, Italy tel. ++39 091 23892208 fax ++39 091 6112023 skype: lavezzimario email: lave...@unipa.it web: http://www.unipa.it/~lavezzi http://www.unipa.it/%7Elavezzi [[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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[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 elements in vector
it works! thank you so much Mario On Fri, Jan 14, 2011 at 1:29 PM, Henrique Dallazuanna www...@gmail.comwrote: Try this: !list %in% c(aa, bb) On Fri, Jan 14, 2011 at 10:19 AM, A M Lavezzi lave...@unipa.it wrote: Hi everybody, I have the following problem. I have a vector containing character elements, such as: list = c(aa,bb,cc,dd,ee) I want to create an index which identifies the elements that are different from, e.g. aa and bb. When I do the following: jj = list!=aa list!=bb jj [1] FALSE FALSE TRUE TRUE TRUE I do the job. But I wonder whether there is a shorter command to compare the elements of 'list' directly to both aa and bb, avoiding the repetition of ' list!=...' and get a result such as jj thanks! -- Andrea Mario Lavezzi currently visiting the University of Oxford, Nuffield College Dipartimento Studi su Politica, Diritto e SocietÄ Piazza Bologni 8 90134 Palermo, Italy tel. ++39 091 23892208 fax ++39 091 6112023 skype: lavezzimario email: lave...@unipa.it web: http://www.unipa.it/~lavezzi http://www.unipa.it/%7Elavezzi [[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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O -- Andrea Mario Lavezzi currently visiting the University of Oxford, Nuffield College Dipartimento Studi su Politica, Diritto e Società Piazza Bologni 8 90134 Palermo, Italy tel. ++39 091 23892208 fax ++39 091 6112023 skype: lavezzimario email: lave...@unipa.it web: http://www.unipa.it/~lavezzi [[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 elements in vector
Try this: !list %in% c(aa, bb) On Fri, Jan 14, 2011 at 10:19 AM, A M Lavezzi lave...@unipa.it wrote: Hi everybody, I have the following problem. I have a vector containing character elements, such as: list = c(aa,bb,cc,dd,ee) I want to create an index which identifies the elements that are different from, e.g. aa and bb. When I do the following: jj = list!=aa list!=bb jj [1] FALSE FALSE TRUE TRUE TRUE I do the job. But I wonder whether there is a shorter command to compare the elements of 'list' directly to both aa and bb, avoiding the repetition of ' list!=...' and get a result such as jj thanks! -- Andrea Mario Lavezzi currently visiting the University of Oxford, Nuffield College Dipartimento Studi su Politica, Diritto e SocietÄ Piazza Bologni 8 90134 Palermo, Italy tel. ++39 091 23892208 fax ++39 091 6112023 skype: lavezzimario email: lave...@unipa.it web: http://www.unipa.it/~lavezzi http://www.unipa.it/%7Elavezzi [[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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[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] Selecting elements in vector
Hi Consider the following code x - rep(1:13, 13) y - 1:3 I want to select all elements in x which are equal to 1, 2 or 3. I know that I could use sel - x==y[1] | x==y[2] | x==y[3] x[sel] to obtain the values, but in my analysis, the y-vector is thousands of elements long. Is there any way, that I can do that easily? Thanks Rainer -- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Plant Conservation Unit Department of Botany University of Cape Town Rondebosch 7701 South Africa __ 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 elements in vector
Use %in%: x [ x %in% y ] G. On Fri, Mar 14, 2008 at 12:37:45PM +0200, Rainer M Krug wrote: Hi Consider the following code x - rep(1:13, 13) y - 1:3 I want to select all elements in x which are equal to 1, 2 or 3. I know that I could use sel - x==y[1] | x==y[2] | x==y[3] x[sel] to obtain the values, but in my analysis, the y-vector is thousands of elements long. Is there any way, that I can do that easily? Thanks Rainer -- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Plant Conservation Unit Department of Botany University of Cape Town Rondebosch 7701 South Africa __ 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. -- Csardi Gabor [EMAIL PROTECTED]UNIL DGM __ 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 elements in vector
Thanks - that is exactly what I was looking for Rainer On Fri, Mar 14, 2008 at 1:10 PM, Henrique Dallazuanna [EMAIL PROTECTED] wrote: Try this: x[x %in% y] On 14/03/2008, Rainer M Krug [EMAIL PROTECTED] wrote: Hi Consider the following code x - rep(1:13, 13) y - 1:3 I want to select all elements in x which are equal to 1, 2 or 3. I know that I could use sel - x==y[1] | x==y[2] | x==y[3] x[sel] to obtain the values, but in my analysis, the y-vector is thousands of elements long. Is there any way, that I can do that easily? Thanks Rainer -- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Plant Conservation Unit Department of Botany University of Cape Town Rondebosch 7701 South Africa __ 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O -- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Plant Conservation Unit Department of Botany University of Cape Town Rondebosch 7701 South Africa __ 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.